feat: get all ads
This commit is contained in:
parent
213d370332
commit
1ae192ccef
15
internal/accessor/ads/get-all.go
Normal file
15
internal/accessor/ads/get-all.go
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package adsrepository
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
adsdomain "legalgo-BE-go/internal/domain/ads"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (a *accessor) GetAll() ([]adsdomain.Ads, error) {
|
||||||
|
var ads []adsdomain.Ads
|
||||||
|
if err := a.db.Find(&ads).Error; err != nil {
|
||||||
|
return ads, fmt.Errorf("failed to get all ads: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ads, nil
|
||||||
|
}
|
||||||
@ -11,6 +11,7 @@ type accessor struct {
|
|||||||
|
|
||||||
type Ads interface {
|
type Ads interface {
|
||||||
Create(adsdomain.Ads) error
|
Create(adsdomain.Ads) error
|
||||||
|
GetAll() ([]adsdomain.Ads, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(
|
func New(
|
||||||
|
|||||||
32
internal/api/http/ads/get_all.go
Normal file
32
internal/api/http/ads/get_all.go
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
package adshttp
|
||||||
|
|
||||||
|
import (
|
||||||
|
adssvc "legalgo-BE-go/internal/services/ads"
|
||||||
|
"legalgo-BE-go/internal/utilities/response"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi/v5"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetAll(
|
||||||
|
router chi.Router,
|
||||||
|
adsSvc adssvc.Ads,
|
||||||
|
) {
|
||||||
|
router.Get("/ads", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
ctx := r.Context()
|
||||||
|
subsPlan, err := adsSvc.GetAll()
|
||||||
|
if err != nil {
|
||||||
|
response.ResponseWithErrorCode(
|
||||||
|
ctx,
|
||||||
|
w,
|
||||||
|
err,
|
||||||
|
response.ErrBadRequest.Code,
|
||||||
|
response.ErrBadRequest.HttpCode,
|
||||||
|
err.Error(),
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
response.RespondJsonSuccess(ctx, w, subsPlan)
|
||||||
|
})
|
||||||
|
}
|
||||||
@ -4,4 +4,5 @@ import "go.uber.org/fx"
|
|||||||
|
|
||||||
var Module = fx.Module("ads-http", fx.Invoke(
|
var Module = fx.Module("ads-http", fx.Invoke(
|
||||||
Create,
|
Create,
|
||||||
|
GetAll,
|
||||||
))
|
))
|
||||||
|
|||||||
@ -45,7 +45,7 @@ func (i *impl) Create(ctx context.Context, spec adsdomain.AdsReq) error {
|
|||||||
Url: spec.Url,
|
Url: spec.Url,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := i.ads.Create(newSpec); err != nil {
|
if err := i.adsRepo.Create(newSpec); err != nil {
|
||||||
if err := i.ossRepo.DeleteObject(ctx, id); err != nil {
|
if err := i.ossRepo.DeleteObject(ctx, id); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
7
internal/services/ads/get_all.go
Normal file
7
internal/services/ads/get_all.go
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package adssvc
|
||||||
|
|
||||||
|
import adsdomain "legalgo-BE-go/internal/domain/ads"
|
||||||
|
|
||||||
|
func (i *impl) GetAll() ([]adsdomain.Ads, error) {
|
||||||
|
return i.adsRepo.GetAll()
|
||||||
|
}
|
||||||
@ -11,22 +11,23 @@ import (
|
|||||||
|
|
||||||
type impl struct {
|
type impl struct {
|
||||||
ossRepo oss.OSSRepository
|
ossRepo oss.OSSRepository
|
||||||
ads adsrepository.Ads
|
adsRepo adsrepository.Ads
|
||||||
validate *validator.Validate
|
validate *validator.Validate
|
||||||
}
|
}
|
||||||
|
|
||||||
type Ads interface {
|
type Ads interface {
|
||||||
Create(context.Context, adsdomain.AdsReq) error
|
Create(context.Context, adsdomain.AdsReq) error
|
||||||
|
GetAll() ([]adsdomain.Ads, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(
|
func New(
|
||||||
ossRepo oss.OSSRepository,
|
ossRepo oss.OSSRepository,
|
||||||
ads adsrepository.Ads,
|
adsRepo adsrepository.Ads,
|
||||||
validate *validator.Validate,
|
validate *validator.Validate,
|
||||||
) Ads {
|
) Ads {
|
||||||
return &impl{
|
return &impl{
|
||||||
ossRepo,
|
ossRepo,
|
||||||
ads,
|
adsRepo,
|
||||||
validate,
|
validate,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user