Require ID, LabelID, User ID for adding label
Add Validation priorty update Update priorty only update priorty Add Support to get tasks via API endpoint
This commit is contained in:
commit
0bd7c08a8b
5 changed files with 75 additions and 24 deletions
56
internal/chore/api.go
Normal file
56
internal/chore/api.go
Normal file
|
@ -0,0 +1,56 @@
|
|||
package chore
|
||||
|
||||
import (
|
||||
"donetick.com/core/config"
|
||||
chRepo "donetick.com/core/internal/chore/repo"
|
||||
"donetick.com/core/internal/utils"
|
||||
jwt "github.com/appleboy/gin-jwt/v2"
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
limiter "github.com/ulule/limiter/v3"
|
||||
|
||||
uRepo "donetick.com/core/internal/user/repo"
|
||||
)
|
||||
|
||||
type API struct {
|
||||
choreRepo *chRepo.ChoreRepository
|
||||
userRepo *uRepo.UserRepository
|
||||
}
|
||||
|
||||
func NewAPI(cr *chRepo.ChoreRepository, userRepo *uRepo.UserRepository) *API {
|
||||
return &API{
|
||||
choreRepo: cr,
|
||||
userRepo: userRepo,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *API) GetAllChores(c *gin.Context) {
|
||||
|
||||
apiToken := c.GetHeader("secretkey")
|
||||
if apiToken == "" {
|
||||
c.JSON(401, gin.H{"error": "Unauthorized"})
|
||||
return
|
||||
}
|
||||
user, err := h.userRepo.GetUserByToken(c, apiToken)
|
||||
if err != nil {
|
||||
c.JSON(401, gin.H{"error": "Unauthorized"})
|
||||
return
|
||||
}
|
||||
chores, err := h.choreRepo.GetChores(c, user.CircleID, user.ID)
|
||||
if err != nil {
|
||||
c.JSON(500, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(200, chores)
|
||||
}
|
||||
|
||||
func APIs(cfg *config.Config, api *API, r *gin.Engine, auth *jwt.GinJWTMiddleware, limiter *limiter.Limiter) {
|
||||
|
||||
thingsAPI := r.Group("api/v1/chore")
|
||||
|
||||
thingsAPI.Use(utils.TimeoutMiddleware(cfg.Server.WriteTimeout), utils.RateLimitMiddleware(limiter))
|
||||
{
|
||||
thingsAPI.GET("", api.GetAllChores)
|
||||
}
|
||||
|
||||
}
|
|
@ -1070,10 +1070,10 @@ func (h *Handler) ModifyHistory(c *gin.Context) {
|
|||
|
||||
func (h *Handler) updatePriority(c *gin.Context) {
|
||||
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 {
|
||||
c.JSON(500, gin.H{
|
||||
"error": "Error getting current user",
|
||||
|
@ -1099,23 +1099,7 @@ func (h *Handler) updatePriority(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
chore, err := h.choreRepo.GetChore(c, id)
|
||||
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 {
|
||||
if err := h.choreRepo.UpdateChorePriority(c, currentUser.ID, id, *priorityReq.Priority); err != nil {
|
||||
c.JSON(500, gin.H{
|
||||
"error": "Error updating priority",
|
||||
})
|
||||
|
@ -1123,7 +1107,7 @@ func (h *Handler) updatePriority(c *gin.Context) {
|
|||
}
|
||||
|
||||
c.JSON(200, gin.H{
|
||||
"res": chore,
|
||||
"message": "Priority updated successfully",
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -107,8 +107,8 @@ type Label struct {
|
|||
}
|
||||
|
||||
type ChoreLabels struct {
|
||||
ChoreID int `json:"choreId" gorm:"primaryKey;autoIncrement:false"`
|
||||
LabelID int `json:"labelId" gorm:"primaryKey;autoIncrement:false"`
|
||||
UserID int `json:"userId" gorm:"primaryKey;autoIncrement:false"`
|
||||
ChoreID int `json:"choreId" gorm:"primaryKey;autoIncrement:false;not null"`
|
||||
LabelID int `json:"labelId" gorm:"primaryKey;autoIncrement:false;not null"`
|
||||
UserID int `json:"userId" gorm:"primaryKey;autoIncrement:false;not null"`
|
||||
Label Label
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package chore
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"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 {
|
||||
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 {
|
||||
return r.db.WithContext(c).Save(&chores).Error
|
||||
}
|
||||
|
|
3
main.go
3
main.go
|
@ -85,11 +85,14 @@ func main() {
|
|||
fx.Provide(thing.NewWebhook),
|
||||
fx.Provide(thing.NewHandler),
|
||||
|
||||
fx.Provide(chore.NewAPI),
|
||||
|
||||
fx.Provide(frontend.NewHandler),
|
||||
|
||||
// fx.Invoke(RunApp),
|
||||
fx.Invoke(
|
||||
chore.Routes,
|
||||
chore.APIs,
|
||||
user.Routes,
|
||||
circle.Routes,
|
||||
thing.Routes,
|
||||
|
|
Loading…
Add table
Reference in a new issue