github.com/shoshinnikita/budget-manager@v0.7.1-0.20220131195411-8c46ff1c6778/internal/web/api/models/models.go (about) 1 // Package models contains models of requests and responses 2 package models 3 4 import ( 5 "errors" 6 "time" 7 8 "github.com/ShoshinNikita/budget-manager/internal/db" 9 ) 10 11 // All requests implement the Request interface 12 type Request interface { 13 request() 14 } 15 16 // BaseRequest is a base request model that implements Request interface. 17 // It must be nested into all requests 18 type BaseRequest struct{} 19 20 func (BaseRequest) request() {} 21 22 // All responses implement the Response interface 23 type Response interface { 24 SetBaseResponse(BaseResponse) 25 } 26 27 // BaseResponse is a base response model that implements Response interface. 28 // It must be nested into all responses 29 type BaseResponse struct { 30 RequestID string `json:"request_id"` 31 Success bool `json:"success"` 32 // Error is specified only when success if false 33 Error string `json:"error,omitempty"` 34 } 35 36 func (r *BaseResponse) SetBaseResponse(newResp BaseResponse) { 37 *r = newResp 38 } 39 40 // ------------------------------------------------- 41 // Month 42 // ------------------------------------------------- 43 44 type GetMonthByDateReq struct { 45 BaseRequest 46 47 Year int `json:"year" validate:"required" example:"2020"` 48 Month time.Month `json:"month" validate:"required" swaggertype:"integer" example:"7"` 49 } 50 51 func (req *GetMonthByDateReq) SanitizeAndCheck() error { 52 if req.Year == 0 { 53 return emptyOrZeroFieldError("year") 54 } 55 if !(time.January <= req.Month && req.Month <= time.December) { 56 return errors.New("invalid month") 57 } 58 return nil 59 } 60 61 type GetMonthResp struct { 62 BaseResponse 63 64 Month db.Month `json:"month"` 65 }