Skip to content

Commit

Permalink
add persian year apis
Browse files Browse the repository at this point in the history
  • Loading branch information
alireza-fa committed Dec 24, 2023
1 parent c1a9507 commit 61f0212
Show file tree
Hide file tree
Showing 8 changed files with 1,107 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func RegisterRouter(r *gin.Engine) {
files := v1.Group("/files", middlewares.Authentication(conf), middlewares.Authorization([]string{"admin"}))
companies := v1.Group("/companies", middlewares.Authentication(conf), middlewares.Authorization([]string{"default"}))
colors := v1.Group("/colors", middlewares.Authentication(conf), middlewares.Authorization([]string{"default"}))
years := v1.Group("/years", middlewares.Authentication(conf), middlewares.Authorization([]string{"default"}))

// Car
carTypes := v1.Group("/car-types", middlewares.Authentication(conf), middlewares.Authorization([]string{"default"}))
Expand All @@ -92,6 +93,7 @@ func RegisterRouter(r *gin.Engine) {
routers.File(files, conf)
routers.Company(companies, conf)
routers.Color(colors, conf)
routers.Year(years, conf)

// Property
routers.PropertyCategory(propertyCategories, conf)
Expand Down
98 changes: 98 additions & 0 deletions src/api/handlers/years.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package handlers

import (
_ "github.com/alireza-fa/golang-car-shop/api/dto"
_ "github.com/alireza-fa/golang-car-shop/api/helper"
"github.com/alireza-fa/golang-car-shop/config"
"github.com/alireza-fa/golang-car-shop/services"
"github.com/gin-gonic/gin"
)

type PersianYearHandler struct {
service *services.PersianYearService
}

func NewPersianYearHandler(cfg *config.Config) *PersianYearHandler {
return &PersianYearHandler{
service: services.NewPersianYearService(cfg),
}
}

// Create PersianYear godoc
// @Summary Create a PersianYear
// @Description Create a PersianYear
// @Tags PersianYears
// @Accept json
// @produces json
// @Param Request body dto.CreatePersianYearRequest true "Create a PersianYear"
// @Success 201 {object} helper.BaseHttpResponse{result=dto.PersianYearResponse} "PersianYear response"
// @Failure 400 {object} helper.BaseHttpResponse "Bad request"
// @Router /v1/years/ [post]
// @Security AuthBearer
func (h *PersianYearHandler) Create(c *gin.Context) {
Create(c, h.service.Create)
}

// Update PersianYear godoc
// @Summary Update a PersianYear
// @Description Update a PersianYear
// @Tags PersianYears
// @Accept json
// @produces json
// @Param id path int true "Id"
// @Param Request body dto.UpdatePersianYearRequest true "Update a PersianYear"
// @Success 200 {object} helper.BaseHttpResponse{result=dto.PersianYearResponse} "PersianYear response"
// @Failure 400 {object} helper.BaseHttpResponse "Bad request"
// @Failure 404 {object} helper.BaseHttpResponse "Not found"
// @Router /v1/years/{id} [put]
// @Security AuthBearer
func (h *PersianYearHandler) Update(c *gin.Context) {
Update(c, h.service.Update)
}

// Delete PersianYear godoc
// @Summary Delete a PersianYear
// @Description Delete a PersianYear
// @Tags PersianYears
// @Accept json
// @produces json
// @Param id path int true "Id"
// @Success 200 {object} helper.BaseHttpResponse "response"
// @Failure 400 {object} helper.BaseHttpResponse "Bad request"
// @Failure 404 {object} helper.BaseHttpResponse "Not found"
// @Router /v1/years/{id} [delete]
// @Security AuthBearer
func (h *PersianYearHandler) Delete(c *gin.Context) {
Delete(c, h.service.Delete)
}

// GetById PersianYear godoc
// @Summary Get a PersianYear
// @Description Get a PersianYear
// @Tags PersianYears
// @Accept json
// @produces json
// @Param id path int true "Id"
// @Success 200 {object} helper.BaseHttpResponse{result=dto.PersianYearResponse} "PersianYear response"
// @Failure 400 {object} helper.BaseHttpResponse "Bad request"
// @Failure 404 {object} helper.BaseHttpResponse "Not found"
// @Router /v1/years/{id} [get]
// @Security AuthBearer
func (h *PersianYearHandler) GetById(c *gin.Context) {
GetById(c, h.service.GetById)
}

// GetByFilter PersianYears godoc
// @Summary Get PersianYears
// @Description Get PersianYears
// @Tags PersianYears
// @Accept json
// @produces json
// @Param Request body dto.PaginationInputWithFilter true "Request"
// @Success 200 {object} helper.BaseHttpResponse{result=dto.PagedList[dto.PersianYearResponse]} "PersianYear response"
// @Failure 400 {object} helper.BaseHttpResponse "Bad request"
// @Router /v1/years/get-by-filter [post]
// @Security AuthBearer
func (h *PersianYearHandler) GetByFilter(c *gin.Context) {
GetByFilter(c, h.service.GetByFilter)
}
10 changes: 10 additions & 0 deletions src/api/routers/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,13 @@ func Color(r *gin.RouterGroup, cfg *config.Config) {
r.GET("/:id", h.GetById)
r.POST("/get-by-filter", h.GetByFilter)
}

func Year(r *gin.RouterGroup, cfg *config.Config) {
h := handlers.NewPersianYearHandler(cfg)

r.POST("/", h.Create)
r.PUT("/:id", h.Update)
r.DELETE("/:id", h.Delete)
r.GET("/:id", h.GetById)
r.POST("/get-by-filter", h.GetByFilter)
}
56 changes: 56 additions & 0 deletions src/data/db/migrations/1_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/alireza-fa/golang-car-shop/pkg/logging"
"golang.org/x/crypto/bcrypt"
"gorm.io/gorm"
"time"
)

var logger = logging.NewLogger(config.GetConfig())
Expand All @@ -22,6 +23,7 @@ func Up_1() {
createCarType(database)
createGearbox(database)
createColor(database)
createYear(database)
}

func createTables(database *gorm.DB) {
Expand Down Expand Up @@ -316,3 +318,57 @@ func createColor(database *gorm.DB) {
database.Create(&models.Color{Name: "Blue", HexCode: "#0000ff"})
}
}

func createYear(database *gorm.DB) {
count := 0
database.
Model(&models.PersianYear{}).
Select("count(*)").
Find(&count)
if count == 0 {

database.Create(&models.PersianYear{
PersianTitle: "1402",
Year: 1402,
StartAt: time.Date(2023, time.Month(3), 21, 0, 0, 0, 0, time.UTC),
EndAt: time.Date(2024, time.Month(3), 20, 0, 0, 0, 0, time.UTC),
})

database.Create(&models.PersianYear{
PersianTitle: "1401",
Year: 1401,
StartAt: time.Date(2022, time.Month(3), 21, 0, 0, 0, 0, time.UTC),
EndAt: time.Date(2023, time.Month(3), 21, 0, 0, 0, 0, time.UTC),
})

database.Create(&models.PersianYear{
PersianTitle: "1400",
Year: 1400,
StartAt: time.Date(2021, time.Month(3), 21, 0, 0, 0, 0, time.UTC),
EndAt: time.Date(2022, time.Month(3), 21, 0, 0, 0, 0, time.UTC),
})

database.Create(&models.PersianYear{
PersianTitle: "1399",
Year: 1399,
StartAt: time.Date(2020, time.Month(3), 20, 0, 0, 0, 0, time.UTC),
EndAt: time.Date(2021, time.Month(3), 21, 0, 0, 0, 0, time.UTC),
})

database.Create(&models.PersianYear{
PersianTitle: "1398",
Year: 1398,
StartAt: time.Date(2019, time.Month(3), 21, 0, 0, 0, 0, time.UTC),
EndAt: time.Date(2020, time.Month(3), 20, 0, 0, 0, 0, time.UTC),
})

database.Create(&models.PersianYear{
PersianTitle: "1398",
Year: 1398,
StartAt: time.Date(2018, time.Month(3), 21, 0, 0, 0, 0, time.UTC),
EndAt: time.Date(2019, time.Month(3), 21, 0, 0, 0, 0, time.UTC),
})
}
}

func Down_1() {}
Loading

0 comments on commit 61f0212

Please sign in to comment.