Skip to content

Commit

Permalink
[olivercodes] feat(gh-2): add team request deletion and confirm delet…
Browse files Browse the repository at this point in the history
…ion endpoint, confirm endpoint checks for requested status
  • Loading branch information
olivercodes committed Jun 27, 2023
1 parent f5bd1f3 commit 2a93ab2
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 27 deletions.
1 change: 1 addition & 0 deletions cmd/teams-api-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func main() {
router.GET("/teams", teamHandler.GetTeams)
router.POST("/teams", teamHandler.AddTeam)
router.DELETE("/teams/:teamID", teamHandler.RemoveTeam)
router.DELETE("/teams/:teamID/confirm", teamHandler.ConfirmRemoveTeam)

router.Run(":8080")
}
2 changes: 1 addition & 1 deletion pkg/domain/teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ type TeamRepository interface {
GetTeams() ([]Team, error)
GetTeam(id string) (Team, error)
AddTeam(newTeam Team) error
// UpdateTeam(team Team) error
UpdateTeam(team Team) error
RemoveTeam(id string) error
}
20 changes: 17 additions & 3 deletions pkg/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,26 @@ func (handler *TeamHandler) GetTeam(c *gin.Context) {

c.IndentedJSON(http.StatusOK, team)
}

func (handler *TeamHandler) RemoveTeam(c *gin.Context) {
teamID := c.Param("teamID")

err := handler.teamService.RemoveTeam(teamID)
err := handler.teamService.RequestRemoveTeam(teamID)
if err != nil {
log.Printf("error %v", err)
c.IndentedJSON(http.StatusInternalServerError, err) // TODO don't return direct errors
c.IndentedJSON(http.StatusInternalServerError, err) // TODO
}

c.IndentedJSON(http.StatusOK, gin.H{"message": "Team delete requested"})
}

func (handler *TeamHandler) ConfirmRemoveTeam(c *gin.Context) {
teamID := c.Param("teamID")

err := handler.teamService.ConfirmRemoveTeam(teamID)
if err != nil {
c.IndentedJSON(http.StatusBadRequest, err.Error()) // TODO
return
}

c.IndentedJSON(http.StatusOK, gin.H{"message": "Team deleted"})
}
37 changes: 18 additions & 19 deletions pkg/repository/redis_teams_api_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,27 +78,11 @@ func (store *RedisTeamRepository) AddTeam(newTeam domain.Team) error {
return store.client.Set(store.ctx, fmt.Sprintf("team:%s", newTeam.TeamID), teamJson, 0).Err()
}

func (store *RedisTeamRepository) RemoveTeam(teamID string) error {
func (store *RedisTeamRepository) UpdateTeam(team domain.Team) error {
// Construct the key for the team
teamKey := fmt.Sprintf("team:%s", teamID)

// Fetch the team from Redis
teamJSON, err := store.client.Get(store.ctx, teamKey).Result()
if err != nil {
return fmt.Errorf("could not fetch team: %v", err)
}
teamKey := fmt.Sprintf("team:%s", team.TeamID)

// Unmarshal the team into a struct
var team domain.Team
err = json.Unmarshal([]byte(teamJSON), &team)
if err != nil {
return fmt.Errorf("could not unmarshal team: %v", err)
}

// Update the MarkedForDeletion field
team.TeamMarkedForDeletion = "Requested"

// Marshal the updated team back into JSON
// Marshal the updated team into JSON
updatedTeamJSON, err := json.Marshal(team)
if err != nil {
return fmt.Errorf("could not marshal team: %v", err)
Expand All @@ -112,3 +96,18 @@ func (store *RedisTeamRepository) RemoveTeam(teamID string) error {

return nil
}

func (store *RedisTeamRepository) RemoveTeam(teamID string) error {

// Construct the key for the team
teamKey := fmt.Sprintf("team:%s", teamID)

// Delete the album from Redis
err := store.client.Del(store.ctx, teamKey).Err()
if err != nil {
return fmt.Errorf("could not delete team: %v", err)
}

return nil

}
43 changes: 39 additions & 4 deletions pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ type TeamService interface {
GetTeam(teamID string) (domain.Team, error)
GetTeams() ([]domain.Team, error)
AddTeam(team domain.Team) error
RemoveTeam(teamID string) error
RequestRemoveTeam(teamID string) error
ConfirmRemoveTeam(teamID string) error
}

type teamServiceImpl struct {
Expand Down Expand Up @@ -47,10 +48,44 @@ func (s *teamServiceImpl) GetTeam(teamID string) (domain.Team, error) {
return team, nil
}

func (s *teamServiceImpl) RemoveTeam(teamID string) error {
err := s.repo.RemoveTeam(teamID)
func (s *teamServiceImpl) UpdateTeam(team domain.Team) error {
err := s.repo.UpdateTeam(team)
if err != nil {
return err
return err // TODO transient/status
}

return nil
}

func (s *teamServiceImpl) RequestRemoveTeam(teamID string) error {
team, err := s.repo.GetTeam(teamID)
if err != nil {
return err // TODO transient/status errors
}

team.TeamMarkedForDeletion = "Requested"

err = s.repo.UpdateTeam(team)
if err != nil {
return err // TODO transient
}

return nil
}

func (s *teamServiceImpl) ConfirmRemoveTeam(teamID string) error {
team, err := s.repo.GetTeam(teamID)
if err != nil {
return err // TODO transient/status errors
}

if team.TeamMarkedForDeletion != "Requested" {
return fmt.Errorf("Team %s is not requested for deletion", teamID)
}

err = s.repo.RemoveTeam(teamID)
if err != nil {
return err // TODO transient
}

return nil
Expand Down

0 comments on commit 2a93ab2

Please sign in to comment.