github.com/shoshinnikita/budget-manager@v0.7.1-0.20220131195411-8c46ff1c6778/internal/web/api/models/income.go (about)

     1  package models
     2  
     3  type AddIncomeReq struct {
     4  	BaseRequest
     5  
     6  	MonthID uint    `json:"month_id" validate:"required" example:"1"`
     7  	Title   string  `json:"title" validate:"required" example:"Salary"`
     8  	Notes   string  `json:"notes"`
     9  	Income  float64 `json:"income" validate:"required" example:"10000"`
    10  }
    11  
    12  func (req *AddIncomeReq) SanitizeAndCheck() error {
    13  	sanitizeString(&req.Title)
    14  	sanitizeString(&req.Notes)
    15  
    16  	if req.MonthID == 0 {
    17  		return emptyOrZeroFieldError("month_id")
    18  	}
    19  	if req.Title == "" {
    20  		return emptyFieldError("title")
    21  	}
    22  	// Skip Notes
    23  	if req.Income <= 0 {
    24  		return notPositiveFieldError("income")
    25  	}
    26  	return nil
    27  }
    28  
    29  type AddIncomeResp struct {
    30  	BaseResponse
    31  
    32  	ID uint `json:"id"`
    33  }
    34  
    35  type EditIncomeReq struct {
    36  	BaseRequest
    37  
    38  	ID     uint     `json:"id" validate:"required" example:"1"`
    39  	Title  *string  `json:"title"`
    40  	Notes  *string  `json:"notes"`
    41  	Income *float64 `json:"income"`
    42  }
    43  
    44  func (req *EditIncomeReq) SanitizeAndCheck() error {
    45  	sanitizeString(req.Title)
    46  	sanitizeString(req.Notes)
    47  
    48  	if req.ID == 0 {
    49  		return emptyOrZeroFieldError("id")
    50  	}
    51  	if req.Title != nil && *req.Title == "" {
    52  		return emptyFieldError("title")
    53  	}
    54  	// Skip Notes
    55  	if req.Income != nil && *req.Income <= 0 {
    56  		return notPositiveFieldError("income")
    57  	}
    58  	return nil
    59  }
    60  
    61  type RemoveIncomeReq struct {
    62  	BaseRequest
    63  
    64  	ID uint `json:"id" validate:"required" example:"1"`
    65  }
    66  
    67  func (req *RemoveIncomeReq) SanitizeAndCheck() error {
    68  	if req.ID == 0 {
    69  		return emptyOrZeroFieldError("id")
    70  	}
    71  	return nil
    72  }