Refactor handlers/income.go
This commit is contained in:
parent
78d420e1d2
commit
21afd95d98
|
@ -1,15 +1,21 @@
|
||||||
package handlers
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"git.qowevisa.me/Qowevisa/fin-check-api/db"
|
"git.qowevisa.me/Qowevisa/fin-check-api/db"
|
||||||
"git.qowevisa.me/Qowevisa/fin-check-api/types"
|
"git.qowevisa.me/Qowevisa/fin-check-api/types"
|
||||||
"git.qowevisa.me/Qowevisa/fin-check-api/utils"
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var incomeTransform func(inp *db.Income) types.DbIncome = func(inp *db.Income) types.DbIncome {
|
||||||
|
return types.DbIncome{
|
||||||
|
ID: inp.ID,
|
||||||
|
CardID: inp.CardID,
|
||||||
|
Comment: inp.Comment,
|
||||||
|
Value: inp.Value,
|
||||||
|
Date: inp.Date,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// @Summary Get income by id
|
// @Summary Get income by id
|
||||||
// @Description Get income by id
|
// @Description Get income by id
|
||||||
// @Tags income
|
// @Tags income
|
||||||
|
@ -24,53 +30,7 @@ import (
|
||||||
// @Security ApiKeyAuth
|
// @Security ApiKeyAuth
|
||||||
// @Router /income/:id [get]
|
// @Router /income/:id [get]
|
||||||
func IncomeGetId(c *gin.Context) {
|
func IncomeGetId(c *gin.Context) {
|
||||||
userIDAny, exists := c.Get("UserID")
|
GetHandler(incomeTransform)(c)
|
||||||
if !exists {
|
|
||||||
c.JSON(500, types.ErrorResponse{Message: "Internal error 001"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var userID uint
|
|
||||||
if userIDVal, ok := userIDAny.(uint); !ok {
|
|
||||||
c.JSON(500, types.ErrorResponse{Message: "Internal error 002"})
|
|
||||||
return
|
|
||||||
} else {
|
|
||||||
userID = userIDVal
|
|
||||||
}
|
|
||||||
|
|
||||||
idStr := c.Param("id")
|
|
||||||
var id uint
|
|
||||||
if idVal, err := strconv.ParseUint(idStr, 10, 32); err != nil {
|
|
||||||
c.JSON(400, types.ErrorResponse{Message: "Invalid request"})
|
|
||||||
return
|
|
||||||
} else {
|
|
||||||
id = uint(idVal)
|
|
||||||
}
|
|
||||||
|
|
||||||
var dbIncome db.Income
|
|
||||||
dbc := db.Connect()
|
|
||||||
if err := dbc.Find(&dbIncome, id).Error; err != nil {
|
|
||||||
c.JSON(500, types.ErrorResponse{Message: err.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if dbIncome.ID == 0 {
|
|
||||||
c.JSON(500, types.ErrorResponse{Message: "DAFUQ003"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if dbIncome.UserID != userID {
|
|
||||||
c.JSON(401, types.ErrorResponse{Message: "This income.id is not yours, you sneaky."})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
ret := types.DbIncome{
|
|
||||||
ID: dbIncome.ID,
|
|
||||||
CardID: dbIncome.CardID,
|
|
||||||
Comment: dbIncome.Comment,
|
|
||||||
Value: dbIncome.Value,
|
|
||||||
Date: dbIncome.Date,
|
|
||||||
UserID: dbIncome.UserID,
|
|
||||||
}
|
|
||||||
c.JSON(200, ret)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// @Summary Add income
|
// @Summary Add income
|
||||||
|
@ -87,52 +47,15 @@ func IncomeGetId(c *gin.Context) {
|
||||||
// @Security ApiKeyAuth
|
// @Security ApiKeyAuth
|
||||||
// @Router /income/add [post]
|
// @Router /income/add [post]
|
||||||
func IncomeAdd(c *gin.Context) {
|
func IncomeAdd(c *gin.Context) {
|
||||||
userIDAny, exists := c.Get("UserID")
|
CreateHandler(
|
||||||
if !exists {
|
&db.Income{},
|
||||||
c.JSON(500, types.ErrorResponse{Message: "Internal error 001"})
|
func(src types.DbIncome, dst *db.Income) {
|
||||||
return
|
dst.CardID = src.CardID
|
||||||
}
|
dst.Value = src.Value
|
||||||
|
dst.Comment = src.Comment
|
||||||
var userID uint
|
dst.Date = src.Date
|
||||||
if userIDVal, ok := userIDAny.(uint); !ok {
|
},
|
||||||
c.JSON(500, types.ErrorResponse{Message: "Internal error 002"})
|
)
|
||||||
return
|
|
||||||
} else {
|
|
||||||
userID = userIDVal
|
|
||||||
}
|
|
||||||
|
|
||||||
var income types.DbIncome
|
|
||||||
if err := c.ShouldBindJSON(&income); err != nil {
|
|
||||||
c.JSON(400, types.ErrorResponse{Message: "Invalid request"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if income.UserID != 0 && userID != income.UserID {
|
|
||||||
c.JSON(403, types.ErrorResponse{Message: "UserID in body is different than yours!"})
|
|
||||||
}
|
|
||||||
if income.UserID == 0 {
|
|
||||||
income.UserID = userID
|
|
||||||
}
|
|
||||||
|
|
||||||
dbIncome := &db.Income{
|
|
||||||
CardID: income.CardID,
|
|
||||||
Comment: income.Comment,
|
|
||||||
Value: income.Value,
|
|
||||||
Date: income.Date,
|
|
||||||
UserID: income.UserID,
|
|
||||||
}
|
|
||||||
dbc := db.Connect()
|
|
||||||
if err := dbc.Create(&dbIncome).Error; err != nil {
|
|
||||||
c.JSON(500, types.ErrorResponse{Message: err.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if dbIncome.ID == 0 {
|
|
||||||
c.JSON(500, types.ErrorResponse{Message: "DAFUQ004"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
msg := types.Message{
|
|
||||||
Info: fmt.Sprintf("Income with id %d was successfully created!", dbIncome.ID),
|
|
||||||
}
|
|
||||||
c.JSON(200, msg)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// @Summary Edit income by id
|
// @Summary Edit income by id
|
||||||
|
@ -150,65 +73,15 @@ func IncomeAdd(c *gin.Context) {
|
||||||
// @Security ApiKeyAuth
|
// @Security ApiKeyAuth
|
||||||
// @Router /income/edit/:id [put]
|
// @Router /income/edit/:id [put]
|
||||||
func IncomePutId(c *gin.Context) {
|
func IncomePutId(c *gin.Context) {
|
||||||
userIDAny, exists := c.Get("UserID")
|
UpdateHandler(
|
||||||
if !exists {
|
func(src types.DbIncome, dst *db.Income) {
|
||||||
c.JSON(500, types.ErrorResponse{Message: "Internal error 001"})
|
dst.CardID = src.CardID
|
||||||
return
|
dst.Value = src.Value
|
||||||
}
|
dst.Comment = src.Comment
|
||||||
|
dst.Date = src.Date
|
||||||
var userID uint
|
},
|
||||||
if userIDVal, ok := userIDAny.(uint); !ok {
|
incomeTransform,
|
||||||
c.JSON(500, types.ErrorResponse{Message: "Internal error 002"})
|
)(c)
|
||||||
return
|
|
||||||
} else {
|
|
||||||
userID = userIDVal
|
|
||||||
}
|
|
||||||
|
|
||||||
idStr := c.Param("id")
|
|
||||||
var id uint
|
|
||||||
if idVal, err := strconv.ParseUint(idStr, 10, 32); err != nil {
|
|
||||||
c.JSON(400, types.ErrorResponse{Message: "Invalid request"})
|
|
||||||
return
|
|
||||||
} else {
|
|
||||||
id = uint(idVal)
|
|
||||||
}
|
|
||||||
|
|
||||||
var dbIncome db.Income
|
|
||||||
dbc := db.Connect()
|
|
||||||
if err := dbc.Find(&dbIncome, id).Error; err != nil {
|
|
||||||
c.JSON(500, types.ErrorResponse{Message: err.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if dbIncome.ID == 0 {
|
|
||||||
c.JSON(500, types.ErrorResponse{Message: "DAFUQ003"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if dbIncome.UserID != userID {
|
|
||||||
c.JSON(401, types.ErrorResponse{Message: "This income.id is not yours, you sneaky."})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
var income types.DbIncome
|
|
||||||
if err := c.ShouldBindJSON(&income); err != nil {
|
|
||||||
c.JSON(400, types.ErrorResponse{Message: "Invalid request"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
utils.MergeNonZeroFields(income, dbIncome)
|
|
||||||
|
|
||||||
if err := dbc.Save(dbIncome).Error; err != nil {
|
|
||||||
c.JSON(500, types.ErrorResponse{Message: err.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
ret := types.DbIncome{
|
|
||||||
ID: dbIncome.ID,
|
|
||||||
CardID: dbIncome.CardID,
|
|
||||||
Comment: dbIncome.Comment,
|
|
||||||
Value: dbIncome.Value,
|
|
||||||
Date: dbIncome.Date,
|
|
||||||
UserID: dbIncome.UserID,
|
|
||||||
}
|
|
||||||
c.JSON(200, ret)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// @Summary Delete income by id
|
// @Summary Delete income by id
|
||||||
|
@ -225,55 +98,5 @@ func IncomePutId(c *gin.Context) {
|
||||||
// @Security ApiKeyAuth
|
// @Security ApiKeyAuth
|
||||||
// @Router /income/delete/:id [delete]
|
// @Router /income/delete/:id [delete]
|
||||||
func IncomeDeleteId(c *gin.Context) {
|
func IncomeDeleteId(c *gin.Context) {
|
||||||
userIDAny, exists := c.Get("UserID")
|
DeleteHandler[*db.Income]()(c)
|
||||||
if !exists {
|
|
||||||
c.JSON(500, types.ErrorResponse{Message: "Internal error 001"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var userID uint
|
|
||||||
if userIDVal, ok := userIDAny.(uint); !ok {
|
|
||||||
c.JSON(500, types.ErrorResponse{Message: "Internal error 002"})
|
|
||||||
return
|
|
||||||
} else {
|
|
||||||
userID = userIDVal
|
|
||||||
}
|
|
||||||
|
|
||||||
idStr := c.Param("id")
|
|
||||||
var id uint
|
|
||||||
if idVal, err := strconv.ParseUint(idStr, 10, 32); err != nil {
|
|
||||||
c.JSON(400, types.ErrorResponse{Message: "Invalid request"})
|
|
||||||
return
|
|
||||||
} else {
|
|
||||||
id = uint(idVal)
|
|
||||||
}
|
|
||||||
|
|
||||||
var dbIncome db.Income
|
|
||||||
dbc := db.Connect()
|
|
||||||
if err := dbc.Find(&dbIncome, id).Error; err != nil {
|
|
||||||
c.JSON(500, types.ErrorResponse{Message: err.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if dbIncome.ID == 0 {
|
|
||||||
c.JSON(500, types.ErrorResponse{Message: "DAFUQ003"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if dbIncome.UserID != userID {
|
|
||||||
c.JSON(401, types.ErrorResponse{Message: "This income.id is not yours, you sneaky."})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if err := dbc.Delete(dbIncome).Error; err != nil {
|
|
||||||
c.JSON(500, types.ErrorResponse{Message: err.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
ret := types.DbIncome{
|
|
||||||
ID: dbIncome.ID,
|
|
||||||
CardID: dbIncome.CardID,
|
|
||||||
Comment: dbIncome.Comment,
|
|
||||||
Value: dbIncome.Value,
|
|
||||||
Date: dbIncome.Date,
|
|
||||||
UserID: dbIncome.UserID,
|
|
||||||
}
|
|
||||||
c.JSON(200, ret)
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user