Update Priority as standalone endpoint
This commit is contained in:
parent
a0265de264
commit
46a5549e53
2 changed files with 13 additions and 21 deletions
|
@ -1070,10 +1070,10 @@ func (h *Handler) ModifyHistory(c *gin.Context) {
|
||||||
|
|
||||||
func (h *Handler) updatePriority(c *gin.Context) {
|
func (h *Handler) updatePriority(c *gin.Context) {
|
||||||
type PriorityReq struct {
|
type PriorityReq struct {
|
||||||
Priority *int `json:"priority" binding:"required,gt=-1"`
|
Priority *int `json:"priority" binding:"required,gt=-1,lt=5"`
|
||||||
}
|
}
|
||||||
|
|
||||||
currrentUser, ok := auth.CurrentUser(c)
|
currentUser, ok := auth.CurrentUser(c)
|
||||||
if !ok {
|
if !ok {
|
||||||
c.JSON(500, gin.H{
|
c.JSON(500, gin.H{
|
||||||
"error": "Error getting current user",
|
"error": "Error getting current user",
|
||||||
|
@ -1099,23 +1099,7 @@ func (h *Handler) updatePriority(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
chore, err := h.choreRepo.GetChore(c, id)
|
if err := h.choreRepo.UpdateChorePriority(c, currentUser.ID, id, *priorityReq.Priority); err != nil {
|
||||||
if err != nil {
|
|
||||||
c.JSON(500, gin.H{
|
|
||||||
"error": "Error getting chore",
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if currrentUser.ID != chore.CreatedBy {
|
|
||||||
c.JSON(403, gin.H{
|
|
||||||
"error": "You are not allowed to update this chore",
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
chore.Priority = *priorityReq.Priority
|
|
||||||
if err := h.choreRepo.UpsertChore(c, chore); err != nil {
|
|
||||||
c.JSON(500, gin.H{
|
c.JSON(500, gin.H{
|
||||||
"error": "Error updating priority",
|
"error": "Error updating priority",
|
||||||
})
|
})
|
||||||
|
@ -1123,7 +1107,7 @@ func (h *Handler) updatePriority(c *gin.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(200, gin.H{
|
c.JSON(200, gin.H{
|
||||||
"res": chore,
|
"message": "Priority updated successfully",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ package chore
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -22,7 +23,14 @@ func NewChoreRepository(db *gorm.DB, cfg *config.Config) *ChoreRepository {
|
||||||
func (r *ChoreRepository) UpsertChore(c context.Context, chore *chModel.Chore) error {
|
func (r *ChoreRepository) UpsertChore(c context.Context, chore *chModel.Chore) error {
|
||||||
return r.db.WithContext(c).Model(&chore).Save(chore).Error
|
return r.db.WithContext(c).Model(&chore).Save(chore).Error
|
||||||
}
|
}
|
||||||
|
func (r *ChoreRepository) UpdateChorePriority(c context.Context, userID int, choreID int, priority int) error {
|
||||||
|
var affectedRows int64
|
||||||
|
r.db.WithContext(c).Model(&chModel.Chore{}).Where("id = ? and created_by = ?", choreID, userID).Update("priority", priority).Count(&affectedRows)
|
||||||
|
if affectedRows == 0 {
|
||||||
|
return errors.New("no rows affected")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
func (r *ChoreRepository) UpdateChores(c context.Context, chores []*chModel.Chore) error {
|
func (r *ChoreRepository) UpdateChores(c context.Context, chores []*chModel.Chore) error {
|
||||||
return r.db.WithContext(c).Save(&chores).Error
|
return r.db.WithContext(c).Save(&chores).Error
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue