Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: group mission user preload #64

Merged
merged 7 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/internal/model/group_user_mission.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down
12 changes: 6 additions & 6 deletions api/internal/server/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down
24 changes: 19 additions & 5 deletions api/internal/server/mission.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion api/internal/server/stratagem.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
Expand Down
Loading