feat: update news
This commit is contained in:
@@ -13,6 +13,7 @@ type News interface {
|
||||
GetAll() ([]newsdomain.News, error)
|
||||
GetBySlug(string) (*newsdomain.News, error)
|
||||
Create(newsdomain.News) error
|
||||
Update(newsdomain.News) error
|
||||
Delete(string) error
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
package newsrepository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
newsdomain "legalgo-BE-go/internal/domain/news"
|
||||
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
func (a *accessor) Update(spec newsdomain.News) error {
|
||||
tx := a.db.Begin()
|
||||
if err := tx.Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "id"}},
|
||||
DoUpdates: clause.AssignmentColumns([]string{
|
||||
"title",
|
||||
"content",
|
||||
"featured_image",
|
||||
"is_premium",
|
||||
"slug",
|
||||
"author_id",
|
||||
"live_at",
|
||||
"updated_at",
|
||||
}),
|
||||
}).Select(
|
||||
"title",
|
||||
"content",
|
||||
"featured_image",
|
||||
"is_premium",
|
||||
"slug",
|
||||
"author_id",
|
||||
"live_at",
|
||||
"updated_at",
|
||||
).Save(&spec).Error; err != nil {
|
||||
tx.Rollback()
|
||||
return fmt.Errorf("failed to update news: %v", err)
|
||||
}
|
||||
if len(spec.Tags) < 1 {
|
||||
if err := tx.Model(&spec).Association("Tags").Clear(); err != nil {
|
||||
tx.Rollback()
|
||||
return fmt.Errorf("failed to clear tags: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if len(spec.Categories) < 1 {
|
||||
if err := tx.Model(&spec).Association("Categories").Clear(); err != nil {
|
||||
tx.Rollback()
|
||||
return fmt.Errorf("failed to clear categories: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := tx.Model(&spec).Association("Tags").Append(spec.Tags); err != nil {
|
||||
tx.Rollback()
|
||||
return fmt.Errorf("failed to add tags: %v", err)
|
||||
}
|
||||
|
||||
if err := tx.Model(&spec).Association("Categories").Append(spec.Categories); err != nil {
|
||||
tx.Rollback()
|
||||
return fmt.Errorf("failed to add categories: %v", err)
|
||||
}
|
||||
|
||||
if err := tx.Commit().Error; err != nil {
|
||||
return fmt.Errorf("failed to commit transaction: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -4,8 +4,8 @@ import (
|
||||
userdomain "legalgo-BE-go/internal/domain/user"
|
||||
)
|
||||
|
||||
func (ur *UserRepository) CreateUser(spec userdomain.User) error {
|
||||
if err := ur.DB.Create(&spec).Error; err != nil {
|
||||
func (ur *accessor) CreateUser(spec userdomain.User) error {
|
||||
if err := ur.db.Create(&spec).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@@ -7,14 +7,14 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func (ur *UserRepository) GetUserByEmail(email string) (*userdomain.User, error) {
|
||||
func (ur *accessor) GetUserByEmail(email string) (*userdomain.User, error) {
|
||||
var user *userdomain.User
|
||||
|
||||
if email == "" {
|
||||
return nil, errors.New("email is empty")
|
||||
}
|
||||
|
||||
if err := ur.DB.First(&user, "email = ?", email).Error; err != nil {
|
||||
if err := ur.db.First(&user, "email = ?", email).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, errors.New("user not found")
|
||||
}
|
||||
|
||||
@@ -6,17 +6,17 @@ import (
|
||||
userdomain "legalgo-BE-go/internal/domain/user"
|
||||
)
|
||||
|
||||
func (ur *UserRepository) GetUserByID(email string) (*userdomain.User, error) {
|
||||
func (ur *accessor) GetUserByID(id string) (*userdomain.User, error) {
|
||||
var user userdomain.User
|
||||
|
||||
if email == "" {
|
||||
return nil, errors.New("email is empty")
|
||||
if id == "" {
|
||||
return nil, errors.New("id is empty")
|
||||
}
|
||||
|
||||
if err := ur.DB.
|
||||
if err := ur.db.
|
||||
Preload("Subscribe").
|
||||
Preload("Subscribe.SubscribePlan").
|
||||
First(&user, "email = ?", email).Error; err != nil {
|
||||
First(&user, "id = ?", id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
@@ -5,14 +5,14 @@ import (
|
||||
userdomain "legalgo-BE-go/internal/domain/user"
|
||||
)
|
||||
|
||||
func (ur *UserRepository) GetUserProfile(email string) (*userdomain.UserProfile, error) {
|
||||
func (ur *accessor) GetUserProfile(email string) (*userdomain.UserProfile, error) {
|
||||
var user *userdomain.User
|
||||
|
||||
if email == "" {
|
||||
return nil, errors.New("email is empty")
|
||||
}
|
||||
|
||||
if err := ur.DB.
|
||||
if err := ur.db.
|
||||
Preload("Subscribe").
|
||||
Preload("Subscribe.SubscribePlan").
|
||||
First(&user, "email = ?", email).
|
||||
|
||||
@@ -5,11 +5,11 @@ import (
|
||||
userdomain "legalgo-BE-go/internal/domain/user"
|
||||
)
|
||||
|
||||
type UserRepository struct {
|
||||
DB *database.DB
|
||||
type accessor struct {
|
||||
db *database.DB
|
||||
}
|
||||
|
||||
type UserIntf interface {
|
||||
type User interface {
|
||||
GetUserByEmail(string) (*userdomain.User, error)
|
||||
GetUserByID(string) (*userdomain.User, error)
|
||||
GetUserProfile(string) (*userdomain.UserProfile, error)
|
||||
@@ -18,6 +18,6 @@ type UserIntf interface {
|
||||
|
||||
func New(
|
||||
db *database.DB,
|
||||
) UserIntf {
|
||||
return &UserRepository{db}
|
||||
) User {
|
||||
return &accessor{db}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user