From 41529b1d6938c52fcbc52aa50a186a0d368d70e5 Mon Sep 17 00:00:00 2001 From: Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> Date: Thu, 29 Jun 2023 18:57:39 +0300 Subject: feat: return back to single map summary edit Former-commit-id: 853dd376267ea63e67b1e4e7041f4c5c5f1be380 --- backend/controllers/modController.go | 45 ++++++++++++++++-------------------- backend/models/requests.go | 19 +++++++-------- 2 files changed, 28 insertions(+), 36 deletions(-) (limited to 'backend') diff --git a/backend/controllers/modController.go b/backend/controllers/modController.go index 7c258ef..5fe0f68 100644 --- a/backend/controllers/modController.go +++ b/backend/controllers/modController.go @@ -55,34 +55,29 @@ func EditMapSummary(c *gin.Context) { return } defer tx.Rollback() - if request.Image != "" { - tx.Exec(`UPDATE maps m SET image = $2 WHERE m.id = $1`, mapID, request.Image) - } - for _, route := range request.Routes { - // Fetch route category and score count - var categoryID, scoreCount int - sql := `SELECT mr.category_id, mr.score_count + // Fetch route category and score count + var categoryID, scoreCount int + sql := `SELECT mr.category_id, mr.score_count FROM map_routes mr INNER JOIN maps m WHERE m.id = $1 AND mr.id = $2` - err = database.DB.QueryRow(sql, mapID, route.RouteID).Scan(&categoryID, &scoreCount) - if err != nil { - c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) - return - } - // Update database with new data - sql = `UPDATE map_routes SET score_count = $2, description = $3, showcase = $4 WHERE id = $1` - _, err = tx.Exec(sql, route.RouteID, route.ScoreCount, route.Description, route.Showcase) - if err != nil { - c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) - return - } - sql = `UPDATE map_history SET user_name = $3, score_count = $4, record_date = $5 WHERE map_id = $1 AND category_id = $2` - _, err = tx.Exec(sql, mapID, categoryID, route.UserName, route.ScoreCount, route.RecordDate) - if err != nil { - c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) - return - } + err = database.DB.QueryRow(sql, mapID, request.RouteID).Scan(&categoryID, &scoreCount) + if err != nil { + c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) + return + } + // Update database with new data + sql = `UPDATE map_routes SET score_count = $2, description = $3, showcase = $4 WHERE id = $1` + _, err = tx.Exec(sql, request.RouteID, request.ScoreCount, request.Description, request.Showcase) + if err != nil { + c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) + return + } + sql = `UPDATE map_history SET user_name = $3, score_count = $4, record_date = $5 WHERE map_id = $1 AND category_id = $2` + _, err = tx.Exec(sql, mapID, categoryID, request.UserName, request.ScoreCount, request.RecordDate) + if err != nil { + c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) + return } if err = tx.Commit(); err != nil { c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) diff --git a/backend/models/requests.go b/backend/models/requests.go index 7a00567..767b4d8 100644 --- a/backend/models/requests.go +++ b/backend/models/requests.go @@ -5,12 +5,16 @@ import ( "time" ) -type EditMapSummaryRequest struct { - Image string `json:"image" binding:"required"` - Routes []EditMapSummaryRequestDetails `json:"routes" binding:"dive"` +type CreateMapSummaryRequest struct { + CategoryID int `json:"category_id" binding:"required"` + Description string `json:"description" binding:"required"` + Showcase string `json:"showcase" binding:"required"` + UserName string `json:"user_name" binding:"required"` + ScoreCount int `json:"score_count" binding:"required"` + RecordDate time.Time `json:"record_date" binding:"required"` } -type EditMapSummaryRequestDetails struct { +type EditMapSummaryRequest struct { RouteID int `json:"route_id" binding:"required"` Description string `json:"description" binding:"required"` Showcase string `json:"showcase" binding:"required"` @@ -19,13 +23,6 @@ type EditMapSummaryRequestDetails struct { RecordDate time.Time `json:"record_date" binding:"required"` } -type CreateMapHistoryRequest struct { - CategoryID int `json:"category_id" binding:"required"` - UserName string `json:"user_name" binding:"required"` - ScoreCount int `json:"score_count" binding:"required"` - RecordDate time.Time `json:"record_date" binding:"required"` -} - type RecordRequest struct { HostDemo *multipart.FileHeader `json:"host_demo" form:"host_demo" binding:"required" swaggerignore:"true"` PartnerDemo *multipart.FileHeader `json:"partner_demo" form:"partner_demo" swaggerignore:"true"` -- cgit v1.2.3