diff --git a/api/internal/model/group_user_mission.go b/api/internal/model/group_user_mission.go index b2e3d9c..5d16888 100644 --- a/api/internal/model/group_user_mission.go +++ b/api/internal/model/group_user_mission.go @@ -10,6 +10,7 @@ type GroupUserMission struct { gorm.Model MissionID uint GroupUserID uint + GroupUser GroupUser Stratagems []*Stratagem `gorm:"many2many:group_user_mission_stratagems;constraint:OnDelete:CASCADE"` } diff --git a/api/internal/server/group.go b/api/internal/server/group.go index cca1c1f..9e3e164 100644 --- a/api/internal/server/group.go +++ b/api/internal/server/group.go @@ -118,9 +118,9 @@ func (s *Server) CreateGroup(c *gin.Context) { // BUG: Need to query the group again to preoload the sub-relations (GroupUsers.User) because the Create method doesn't return the sub-relations err = db. Preload("Missions.GroupUserMissions.Stratagems"). + Preload("Missions.GroupUserMissions.GroupUser.User"). Preload("GroupUsers.User"). Preload("GroupUsers.GroupUserMissions.Stratagems"). - Preload("GroupUsers.GroupUserMissions.User"). Preload("Planet"). First(&newGroup, "id = ?", newGroup.ID).Error @@ -150,9 +150,9 @@ func (s *Server) GetGroups(c *gin.Context) { // Get public groups err := db. Preload("Missions.GroupUserMissions.Stratagems"). + Preload("Missions.GroupUserMissions.GroupUser.User"). Preload("GroupUsers.User"). Preload("GroupUsers.GroupUserMissions.Stratagems"). - Preload("GroupUsers.GroupUserMissions.User"). Preload("Planet"). Find(&groups, "public = ?", true).Error @@ -170,9 +170,9 @@ func (s *Server) GetGroups(c *gin.Context) { Joins("LEFT JOIN group_users ON groups.id = group_users.group_id"). Where("group_users.user_id = ? OR groups.public = ?", user.ID, true). Preload("Missions.GroupUserMissions.Stratagems"). + Preload("Missions.GroupUserMissions.GroupUser.User"). Preload("GroupUsers.User"). Preload("GroupUsers.GroupUserMissions.Stratagems"). - Preload("GroupUsers.GroupUserMissions.User"). Preload("Planet"). Distinct(GroupFieldsDistinct). Find(&groups).Error @@ -207,9 +207,9 @@ func (s *Server) GetGroup(c *gin.Context) { if !ok { if err := db. Preload("Missions.GroupUserMissions.Stratagems"). + Preload("Missions.GroupUserMissions.GroupUser.User"). Preload("GroupUsers.User"). Preload("GroupUsers.GroupUserMissions.Stratagems"). - Preload("GroupUsers.GroupUserMissions.User"). Preload("Planet"). First(&group, "id = ? AND public = ?", groupID, true). Error; err != nil { @@ -228,9 +228,9 @@ func (s *Server) GetGroup(c *gin.Context) { Joins("LEFT JOIN group_users ON groups.id = group_users.group_id"). Where("group_users.user_id = ? OR groups.public = ?", user.ID, true). Preload("Missions.GroupUserMissions.Stratagems"). + Preload("Missions.GroupUserMissions.GroupUser.User"). Preload("GroupUsers.User"). Preload("GroupUsers.GroupUserMissions.Stratagems"). - Preload("GroupUsers.GroupUserMissions.User"). Preload("Planet"). Distinct(GroupFieldsDistinct). First(&group, "groups.id = ?", groupID).Error @@ -339,10 +339,10 @@ func (s *Server) UpdateGroup(c *gin.Context) { err = db. Preload("Missions.GroupUserMissions.Stratagems"). + Preload("Missions.GroupUserMissions.GroupUser.User"). Preload("GroupUsers.User"). Preload("GroupUsers.User.TokenFcm"). Preload("GroupUsers.GroupUserMissions.Stratagems"). - Preload("GroupUsers.GroupUserMissions.User"). Preload("Planet"). First(&updatedGroup, "id = ?", group.ID).Error diff --git a/api/internal/server/mission.go b/api/internal/server/mission.go index 41092ae..dcbbd49 100644 --- a/api/internal/server/mission.go +++ b/api/internal/server/mission.go @@ -114,7 +114,7 @@ func (s *Server) CreateMission(c *gin.Context) { if err := db. Preload("GroupUserMissions.Stratagems"). - Preload("GroupUserMissions.User"). + Preload("GroupUserMissions.GroupUser.User"). First(&newMission, "id = ?", newMission.ID). Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { @@ -177,7 +177,7 @@ func (s *Server) GetMission(c *gin.Context) { if err := db. Preload("GroupUserMissions.Stratagems"). - Preload("GroupUserMissions.User"). + Preload("GroupUserMissions.GroupUser.User"). First(&mission, "id = ?", missionID). Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { @@ -274,7 +274,7 @@ func (s *Server) UpdateMission(c *gin.Context) { // Get the updated mission with the group user missions if err := db. Preload("GroupUserMissions.Stratagems"). - Preload("GroupUserMissions.User"). + Preload("GroupUserMissions.GroupUser.User"). First(&updatedMission, "id = ?", missionID). Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { @@ -495,7 +495,7 @@ func (s *Server) JoinMission(c *gin.Context) { if err := db. Preload("Stratagems"). - Preload("User"). + Preload("GroupUser.User"). First(&newGroupUserMission, "id = ?", newGroupUserMission.ID). Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { @@ -725,7 +725,21 @@ func (s *Server) UpdateGroupUserMission(c *gin.Context) { } // Update the group user mission - if err := db.Model(&groupUserMission).Preload("User").Association("Stratagems").Replace(newStratagems); err != nil { + if err := db.Model(&groupUserMission).Association("Stratagems").Replace(newStratagems); err != nil { + s.InternalErrorResponse(c, err) + return + } + + if err := db. + Preload("Stratagems"). + Preload("GroupUser.User"). + First(&groupUserMission, "id = ?", groupUserMission.ID). + Error; err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + s.NotFoundResponse(c, "group user mission") + return + } + s.InternalErrorResponse(c, err) return } diff --git a/api/internal/server/stratagem.go b/api/internal/server/stratagem.go index 1b61f6f..d7b679b 100644 --- a/api/internal/server/stratagem.go +++ b/api/internal/server/stratagem.go @@ -4,6 +4,7 @@ import ( "errors" "net/http" + "github.com/Spacelocust/for-democracy/internal/enum" "github.com/Spacelocust/for-democracy/internal/model" "github.com/gin-gonic/gin" "gorm.io/gorm" @@ -28,7 +29,7 @@ func (s *Server) GetStratagems(c *gin.Context) { var stratagems []model.Stratagem - if err := db.Find(&stratagems).Error; err != nil { + if err := db.Not("use_type = ?", enum.Shared).Find(&stratagems).Error; err != nil { s.InternalErrorResponse(c, err) return }