Compare commits
No commits in common. "1297c71200aef4b43fe8999dc915fbe0372120f3" and "ce17e9dc73e7f229a95e0b3c4f044616f88029d5" have entirely different histories.
1297c71200
...
ce17e9dc73
@ -3,11 +3,9 @@ package database
|
||||
import "time"
|
||||
|
||||
type Ads struct {
|
||||
ID string `gorm:"primaryKey;not null" json:"id"`
|
||||
ImageUrl string `gorm:"not null" json:"image_url"`
|
||||
Url string `gorm:"not null" json:"url"`
|
||||
StartDate *time.Time `gorm:"default:null" json:"start_date"`
|
||||
EndDate *time.Time `gorm:"default:null" json:"end_date"`
|
||||
CreatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"created_at"`
|
||||
UpdatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"updated_at"`
|
||||
ID string `gorm:"primaryKey;not null" json:"id"`
|
||||
ImageUrl string `gorm:"not null" json:"image_url"`
|
||||
Url string `gorm:"not null" json:"url"`
|
||||
CreatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"created_at"`
|
||||
UpdatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"updated_at"`
|
||||
}
|
||||
|
||||
@ -12,7 +12,6 @@ type accessor struct {
|
||||
type Ads interface {
|
||||
Create(adsdomain.Ads) error
|
||||
GetAll() ([]adsdomain.Ads, error)
|
||||
Update(adsdomain.Ads) error
|
||||
Delete(string) error
|
||||
}
|
||||
|
||||
|
||||
@ -1,31 +0,0 @@
|
||||
package adsrepository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
adsdomain "legalgo-BE-go/internal/domain/ads"
|
||||
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
func (a *accessor) Update(spec adsdomain.Ads) error {
|
||||
if err := a.db.Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "id"}},
|
||||
DoUpdates: clause.AssignmentColumns([]string{
|
||||
"image_url",
|
||||
"url",
|
||||
"updated_at",
|
||||
"start_date",
|
||||
"end_date",
|
||||
}),
|
||||
}).Select(
|
||||
"image_url",
|
||||
"url",
|
||||
"updated_at",
|
||||
"start_date",
|
||||
"end_date",
|
||||
).Save(&spec).Error; err != nil {
|
||||
return fmt.Errorf("failed to update tag: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@ -5,6 +5,5 @@ import "go.uber.org/fx"
|
||||
var Module = fx.Module("ads-http", fx.Invoke(
|
||||
Create,
|
||||
GetAll,
|
||||
Update,
|
||||
Delete,
|
||||
))
|
||||
|
||||
@ -1,82 +0,0 @@
|
||||
package adshttp
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
authmiddleware "legalgo-BE-go/internal/api/http/middleware/auth"
|
||||
adsdomain "legalgo-BE-go/internal/domain/ads"
|
||||
adssvc "legalgo-BE-go/internal/services/ads"
|
||||
"legalgo-BE-go/internal/utilities/response"
|
||||
"legalgo-BE-go/internal/utilities/utils"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-playground/validator/v10"
|
||||
)
|
||||
|
||||
func Update(
|
||||
router chi.Router,
|
||||
validate *validator.Validate,
|
||||
adsSvc adssvc.Ads,
|
||||
) {
|
||||
router.
|
||||
With(authmiddleware.Authorize()).
|
||||
Put("/ads/{ads_id}/update", func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
adsID := chi.URLParam(r, "ads_id")
|
||||
|
||||
if adsID == "" {
|
||||
response.RespondJsonErrorWithCode(
|
||||
ctx,
|
||||
w,
|
||||
fmt.Errorf("ads id is not provided"),
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
"ads id is not provided",
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
var spec adsdomain.AdsReq
|
||||
if err := utils.UnmarshalBody(r, &spec); err != nil {
|
||||
response.RespondJsonErrorWithCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
"failed to unmarshal body",
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
if err := validate.Struct(spec); err != nil {
|
||||
response.RespondJsonErrorWithCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
err.(validator.ValidationErrors).Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
if err := adsSvc.Update(adsID, spec); err != nil {
|
||||
response.RespondJsonErrorWithCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
err.Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
response.RespondJsonSuccess(ctx, w, struct {
|
||||
Message string
|
||||
}{
|
||||
Message: "update ads success",
|
||||
})
|
||||
})
|
||||
}
|
||||
@ -5,18 +5,14 @@ import (
|
||||
)
|
||||
|
||||
type Ads struct {
|
||||
ID string `json:"id"`
|
||||
ImageUrl string `json:"image_url"`
|
||||
Url string `json:"url"`
|
||||
StartDate *time.Time `json:"start_date"`
|
||||
EndDate *time.Time `json:"end_date"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
ID string `gorm:"primaryKey;not null" json:"id"`
|
||||
ImageUrl string `gorm:"not null" json:"image_url"`
|
||||
Url string `gorm:"not null" json:"url"`
|
||||
CreatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"created_at"`
|
||||
UpdatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"updated_at"`
|
||||
}
|
||||
|
||||
type AdsReq struct {
|
||||
Image string `json:"image" validate:"required"`
|
||||
URL string `json:"url" validate:"required"`
|
||||
StartDate *time.Time `json:"start_date" validate:"required"`
|
||||
EndDate *time.Time `json:"end_date" validate:"required"`
|
||||
Image string `json:"image" validate:"required"`
|
||||
URL string `json:"url" validate:"required"`
|
||||
}
|
||||
|
||||
@ -18,11 +18,9 @@ func (i *impl) Create(spec adsdomain.AdsReq) error {
|
||||
}
|
||||
|
||||
newSpec := adsdomain.Ads{
|
||||
ID: uuid.NewString(),
|
||||
ImageUrl: spec.Image,
|
||||
Url: spec.URL,
|
||||
StartDate: spec.StartDate,
|
||||
EndDate: spec.EndDate,
|
||||
ID: uuid.NewString(),
|
||||
ImageUrl: spec.Image,
|
||||
Url: spec.URL,
|
||||
}
|
||||
|
||||
return i.adsRepo.Create(newSpec)
|
||||
|
||||
@ -12,7 +12,6 @@ type impl struct {
|
||||
type Ads interface {
|
||||
Create(adsdomain.AdsReq) error
|
||||
GetAll() ([]adsdomain.Ads, error)
|
||||
Update(string, adsdomain.AdsReq) error
|
||||
Delete(string) error
|
||||
}
|
||||
|
||||
|
||||
@ -1,19 +0,0 @@
|
||||
package adssvc
|
||||
|
||||
import (
|
||||
adsdomain "legalgo-BE-go/internal/domain/ads"
|
||||
timeutils "legalgo-BE-go/internal/utilities/time_utils"
|
||||
)
|
||||
|
||||
func (i *impl) Update(adsID string, spec adsdomain.AdsReq) error {
|
||||
newsSpec := adsdomain.Ads{
|
||||
ID: adsID,
|
||||
ImageUrl: spec.Image,
|
||||
Url: spec.URL,
|
||||
UpdatedAt: timeutils.Now(),
|
||||
StartDate: spec.StartDate,
|
||||
EndDate: spec.EndDate,
|
||||
}
|
||||
|
||||
return i.adsRepo.Update(newsSpec)
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user