github.com/shoshinnikita/budget-manager@v0.7.1-0.20220131195411-8c46ff1c6778/internal/web/api/models/spend.go (about) 1 package models 2 3 type AddSpendReq struct { 4 BaseRequest 5 6 DayID uint `json:"day_id" validate:"required" example:"1"` 7 8 Title string `json:"title" validate:"required" example:"Food"` 9 TypeID uint `json:"type_id"` 10 Notes string `json:"notes"` 11 Cost float64 `json:"cost" validate:"required" example:"30"` 12 } 13 14 func (req *AddSpendReq) SanitizeAndCheck() error { 15 sanitizeString(&req.Title) 16 sanitizeString(&req.Notes) 17 18 if req.DayID == 0 { 19 return emptyOrZeroFieldError("day_id") 20 } 21 if req.Title == "" { 22 return emptyFieldError("title") 23 } 24 // Skip Type 25 // Skip Notes 26 if req.Cost < 0 { 27 return negativeFieldError("cost") 28 } 29 return nil 30 } 31 32 type AddSpendResp struct { 33 BaseResponse 34 35 ID uint `json:"id"` 36 } 37 38 type EditSpendReq struct { 39 BaseRequest 40 41 ID uint `json:"id" validate:"required" example:"1"` 42 Title *string `json:"title"` 43 TypeID *uint `json:"type_id"` 44 Notes *string `json:"notes"` 45 Cost *float64 `json:"cost"` 46 } 47 48 func (req *EditSpendReq) SanitizeAndCheck() error { 49 sanitizeString(req.Title) 50 sanitizeString(req.Notes) 51 52 if req.ID == 0 { 53 return emptyOrZeroFieldError("id") 54 } 55 if req.Title != nil && *req.Title == "" { 56 return emptyFieldError("title") 57 } 58 // Skip Type 59 // Skip Notes 60 if req.Cost != nil && *req.Cost < 0 { 61 return negativeFieldError("cost") 62 } 63 return nil 64 } 65 66 type RemoveSpendReq struct { 67 BaseRequest 68 69 ID uint `json:"id" validate:"required" example:"1"` 70 } 71 72 func (req *RemoveSpendReq) SanitizeAndCheck() error { 73 if req.ID == 0 { 74 return emptyOrZeroFieldError("id") 75 } 76 return nil 77 }