feat: create ads

This commit is contained in:
ericprd
2025-03-09 00:47:24 +08:00
parent 13c976dcbf
commit 213d370332
14 changed files with 289 additions and 0 deletions
+56
View File
@@ -0,0 +1,56 @@
package adssvc
import (
"context"
"fmt"
adsdomain "legalgo-BE-go/internal/domain/ads"
"path"
"github.com/google/uuid"
)
func (i *impl) Create(ctx context.Context, spec adsdomain.AdsReq) error {
id := uuid.NewString()
file := spec.Image.FileHeader
spec.Image.FileSize = file.Size
spec.Image.Ext = path.Ext(file.Filename)
if err := i.validate.Struct(spec); err != nil {
return err
}
// Open the file and read its content
srcFile, err := file.Open()
if err != nil {
return err
}
defer srcFile.Close()
fileContent := make([]byte, file.Size)
_, err = srcFile.Read(fileContent)
if err != nil {
return err
}
filePath := fmt.Sprintf("%v/%v%v", spec.Image.FolderName, id, spec.Image.Ext)
fileUrl, err := i.ossRepo.UploadFile(ctx, filePath, fileContent)
if err != nil {
return err
}
newSpec := adsdomain.Ads{
ID: id,
ImageUrl: fileUrl,
Url: spec.Url,
}
if err := i.ads.Create(newSpec); err != nil {
if err := i.ossRepo.DeleteObject(ctx, id); err != nil {
return err
}
return err
}
return nil
}