Skip to content

Commit

Permalink
add year_service
Browse files Browse the repository at this point in the history
  • Loading branch information
alireza-fa committed Dec 24, 2023
1 parent b171c17 commit c1a9507
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 5 deletions.
33 changes: 32 additions & 1 deletion src/api/dto/base.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package dto

import "mime/multipart"
import (
"mime/multipart"
"time"
)

type CreateUpdateCountryRequest struct {
Name string `json:"name" binding:"required,alpha,min=3,max=20"`
Expand Down Expand Up @@ -90,3 +93,31 @@ type ColorResponse struct {
Name string `json:"name,omitempty"`
HexCode string `json:"hexCode,omitempty"`
}

type CreatePersianYearRequest struct {
PersianTitle string `json:"persianTitle" binding:"min=4,max=4"`
Year int `json:"year"`
StartAt time.Time `json:"startAt"`
EndAt time.Time `json:"endAt"`
}

type UpdatePersianYearRequest struct {
PersianTitle string `json:"persianTitle,omitempty" binding:"min=4,max=4"`
Year int `json:"year,omitempty"`
StartAt time.Time `json:"startAt,omitempty"`
EndAt time.Time `json:"endAt,omitempty"`
}

type PersianYearResponse struct {
Id int `json:"id"`
PersianTitle string `json:"persianTitle,omitempty"`
Year int `json:"year,omitempty"`
StartAt time.Time `json:"startAt,omitempty"`
EndAt time.Time `json:"endAt,omitempty"`
}

type PersianYearWithoutDateResponse struct {
Id int `json:"id"`
PersianTitle string `json:"persianTitle,omitempty"`
Year int `json:"year,omitempty"`
}
2 changes: 1 addition & 1 deletion src/api/dto/car.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,5 @@ type UpdateCarModelColorRequest struct {

type CarModelColorResponse struct {
Id int `json:"id"`
Color ColorResponse `json:"color"`
Color ColorResponse `json:"color,omitempty"`
}
6 changes: 3 additions & 3 deletions src/data/models/car.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ type CarModel struct {
type CarModelColor struct {
BaseModel
CarModel CarModel `gorm:"foreignKey:CarModelId;constraint:OnUpdate:NO ACTION;OnDelete:NO ACTION"`
CarModelId int
Color Color `gorm:"foreignKey:ColorId;constraint:OnUpdate:NO ACTION;OnDelete:NO ACTION"`
ColorId int
CarModelId int `gorm:"uniqueIndex:idx_CarModelId_ColorId"`
Color Color `gorm:"foreignKey:ColorId;constraint:OnUpdate:NO ACTION;OnDelete:NO ACTION"`
ColorId int `gorm:"uniqueIndex:idx_CarModelId_ColorId"`
}

type CarModelYear struct {
Expand Down
48 changes: 48 additions & 0 deletions src/services/year_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package services

import (
"context"
"github.com/alireza-fa/golang-car-shop/api/dto"
"github.com/alireza-fa/golang-car-shop/config"
"github.com/alireza-fa/golang-car-shop/data/db"
"github.com/alireza-fa/golang-car-shop/data/models"
"github.com/alireza-fa/golang-car-shop/pkg/logging"
)

type PersianYearService struct {
base *BaseService[models.PersianYear, dto.CreatePersianYearRequest, dto.UpdatePersianYearRequest, dto.PersianYearResponse]
}

func NewPersianYearService(cfg *config.Config) *PersianYearService {
return &PersianYearService{
base: &BaseService[models.PersianYear, dto.CreatePersianYearRequest, dto.UpdatePersianYearRequest, dto.PersianYearResponse]{
Database: db.GetDb(),
Logger: logging.NewLogger(cfg),
},
}
}

// Create year
func (s *PersianYearService) Create(ctx context.Context, req *dto.CreatePersianYearRequest) (*dto.PersianYearResponse, error) {
return s.base.Create(ctx, req)
}

// Update year
func (s *PersianYearService) Update(ctx context.Context, id int, req *dto.UpdatePersianYearRequest) (*dto.PersianYearResponse, error) {
return s.base.Update(ctx, id, req)
}

// Delete year
func (s *PersianYearService) Delete(ctx context.Context, id int) error {
return s.base.Delete(ctx, id)
}

// GetById year
func (s *PersianYearService) GetById(ctx context.Context, id int) (*dto.PersianYearResponse, error) {
return s.base.GetById(ctx, id)
}

// GetByFilter year
func (s *PersianYearService) GetByFilter(ctx context.Context, req *dto.PaginationInputWithFilter) (*dto.PagedList[dto.PersianYearResponse], error) {
return s.base.GetByFilter(ctx, req)
}

0 comments on commit c1a9507

Please sign in to comment.