add fuel subtype to fillup

This commit is contained in:
Akhil Gupta
2021-07-23 16:30:55 +05:30
parent 19ab2a59dd
commit 4ee44fb1f1
10 changed files with 69 additions and 2 deletions

View File

@@ -26,6 +26,7 @@ func RegisterVehicleController(router *gin.RouterGroup) {
router.GET("/me/stats", getMystats)
router.GET("/vehicles/:id/fillups", getFillupsByVehicleId)
router.GET("/vehicles/:id/fuelSubTypes", getFuelSubTypesByVehicleId)
router.POST("/vehicles/:id/fillups", createFillup)
router.GET("/vehicles/:id/fillups/:subId", getFillupById)
router.PUT("/vehicles/:id/fillups/:subId", updateFillup)
@@ -121,6 +122,22 @@ func getFillupsByVehicleId(c *gin.Context) {
c.JSON(http.StatusUnprocessableEntity, common.NewValidatorError(err))
}
}
func getFuelSubTypesByVehicleId(c *gin.Context) {
var searchByIdQuery models.SearchByIdQuery
if err := c.ShouldBindUri(&searchByIdQuery); err == nil {
fuelSubtypes, err := service.GetDistinctFuelSubtypesForVehicle(searchByIdQuery.Id)
if err != nil {
c.JSON(http.StatusUnprocessableEntity, common.NewError("getFuelSubTypesByVehicleId", err))
return
}
c.JSON(http.StatusOK, fuelSubtypes)
} else {
c.JSON(http.StatusUnprocessableEntity, common.NewValidatorError(err))
}
}
func getExpensesByVehicleId(c *gin.Context) {

View File

@@ -119,6 +119,7 @@ type Fillup struct {
Currency string `json:"currency"`
DistanceUnit DistanceUnit `json:"distanceUnit"`
Source string `json:"source"`
FuelSubType string `json:"fuelSubType"`
}
func (v *Fillup) FuelUnitDetail() EnumDetail {

View File

@@ -50,6 +50,7 @@ type CreateFillupRequest struct {
FillingStation string `form:"fillingStation" json:"fillingStation"`
UserID string `form:"userId" json:"userId" binding:"required"`
Date time.Time `form:"date" json:"date" binding:"required" time_format:"2006-01-02"`
FuelSubType string `form:"fuelSubType" json:"fuelSubType"`
}
type UpdateFillupRequest struct {

View File

@@ -139,6 +139,7 @@ func CreateFillup(model models.CreateFillupRequest) (*db.Fillup, error) {
Date: model.Date,
Currency: user.Currency,
DistanceUnit: user.DistanceUnit,
FuelSubType: model.FuelSubType,
Source: "API",
}
@@ -196,6 +197,7 @@ func UpdateFillup(fillupId string, model models.UpdateFillupRequest) error {
Comments: model.Comments,
FillingStation: model.FillingStation,
UserID: model.UserID,
FuelSubType: model.FuelSubType,
Date: model.Date,
}).Error
}
@@ -235,6 +237,11 @@ func GetVehicleAttachments(vehicleId string) (*[]db.Attachment, error) {
return db.GetVehicleAttachments(vehicleId)
}
func GetDistinctFuelSubtypesForVehicle(vehicleId string) ([]string, error) {
var names []string
tx := db.DB.Model(&db.Fillup{}).Where("vehicle_id=? and fuel_sub_type is not null", vehicleId).Distinct().Pluck("fuel_sub_type", &names)
return names, tx.Error
}
func GetUserStats(userId string, model models.UserStatsQueryModel) ([]models.VehicleStatsModel, error) {