diff --git a/internal/services/container.go b/internal/services/container.go index 6436936..c702f93 100644 --- a/internal/services/container.go +++ b/internal/services/container.go @@ -149,17 +149,7 @@ func (c *Container) InitReportManager() { // Create the user service. func (c *Container) InitUserService() { - repository := &struct { - *repository.UserRepository - *repository.ProjectRepository - *repository.CrawlRepository - }{ - c.userRepository, - c.projectRepository, - c.crawlRepository, - } - - c.UserService = NewUserService(repository) + c.UserService = NewUserService(c.userRepository) } // Create the Project service. @@ -173,6 +163,11 @@ func (c *Container) InitProjectService() { } c.ProjectService = NewProjectService(repository) + + // UserService DeleteHooks are called when a user is deleted. + // Add a DeleteHook so it deletes all user projects and crawl + // data when a user is deleted. + c.UserService.AddDeleteHook(c.ProjectService.DeleteAllUserProjects) } // Create the ProjectView service. diff --git a/internal/services/project.go b/internal/services/project.go index 673ed5f..8fe1ec2 100644 --- a/internal/services/project.go +++ b/internal/services/project.go @@ -15,6 +15,7 @@ type ( DisableProject(*models.Project) UpdateProject(p *models.Project) error FindProjectById(id int, uid int) (models.Project, error) + FindProjectsByUser(userId int) []models.Project DeleteProjectCrawls(*models.Project) } @@ -78,3 +79,12 @@ func (s *ProjectService) DeleteProject(p *models.Project) { func (s *ProjectService) UpdateProject(p *models.Project) error { return s.repository.UpdateProject(p) } + +// Delete all user projects and crawl data. +func (s *ProjectService) DeleteAllUserProjects(user *models.User) { + projects := s.repository.FindProjectsByUser(user.Id) + for _, p := range projects { + s.repository.DeleteProjectCrawls(&p) + s.repository.DeleteProject(&p) + } +} diff --git a/internal/services/project_test.go b/internal/services/project_test.go index 3c7b218..83a46cf 100644 --- a/internal/services/project_test.go +++ b/internal/services/project_test.go @@ -21,8 +21,9 @@ type projectTestRepository struct{} func (s *projectTestRepository) SaveProject(project *models.Project, userId int) {} func (s *projectTestRepository) DeleteProject(project *models.Project) {} func (s *projectTestRepository) DisableProject(project *models.Project) {} -func (s *projectTestRepository) UpdateProject(p *models.Project) error { - return nil +func (s *projectTestRepository) UpdateProject(p *models.Project) error { return nil } +func (p *projectTestRepository) FindProjectsByUser(uid int) []models.Project { + return []models.Project{} } func (s *projectTestRepository) FindProjectById(id, uid int) (models.Project, error) { p := models.Project{} diff --git a/internal/services/user.go b/internal/services/user.go index 72650e6..c8e11a5 100644 --- a/internal/services/user.go +++ b/internal/services/user.go @@ -27,21 +27,19 @@ var ( ) type ( + DeleteHook func(user *models.User) + UserServiceRepository interface { UserSignup(email, hashedPassword string) (*models.User, error) FindUserByEmail(email string) (*models.User, error) UserUpdatePassword(email, hashedPassword string) error DeleteUser(user *models.User) error DisableUser(user *models.User) error - - DeleteProjectCrawls(project *models.Project) - - DeleteProject(project *models.Project) - FindProjectsByUser(userId int) []models.Project } UserService struct { - repository UserServiceRepository + repository UserServiceRepository + deleteHooks []DeleteHook } ) @@ -120,16 +118,20 @@ func (s *UserService) UpdatePassword(user *models.User, currentPassword, newPass func (s *UserService) DeleteUser(user *models.User) { s.repository.DisableUser(user) go func() { - projects := s.repository.FindProjectsByUser(user.Id) - for _, p := range projects { - s.repository.DeleteProjectCrawls(&p) - s.repository.DeleteProject(&p) + for _, h := range s.deleteHooks { + h(user) } s.repository.DeleteUser(user) }() } +// AddDeleteHook adds a new hook function that will be called when the user is deleted. +// This is used for user data clean up. +func (s *UserService) AddDeleteHook(hook DeleteHook) { + s.deleteHooks = append(s.deleteHooks, hook) +} + // Validate the password to make sure it follows certain criteria. func (s *UserService) validPassword(password string) bool { return len(password) > 1 diff --git a/internal/services/user_test.go b/internal/services/user_test.go index 5a850a8..87e5e51 100644 --- a/internal/services/user_test.go +++ b/internal/services/user_test.go @@ -56,11 +56,6 @@ func (s *userTestRepository) DeleteUser(u *models.User) error { func (s *userTestRepository) DisableUser(*models.User) error { return nil } -func (p *userTestRepository) DeleteProject(*models.Project) {} -func (p *userTestRepository) FindProjectsByUser(uid int) []models.Project { - return []models.Project{} -} -func (p *userTestRepository) DeleteProjectCrawls(*models.Project) {} var userService = services.NewUserService(&userTestRepository{})