105 lines
2.6 KiB
Go
105 lines
2.6 KiB
Go
package biz
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"gitlab.guxuan.icu/jinshan_community/internal/mods/common/dal"
|
|
"gitlab.guxuan.icu/jinshan_community/internal/mods/common/schema"
|
|
"gitlab.guxuan.icu/jinshan_community/pkg/errors"
|
|
"gitlab.guxuan.icu/jinshan_community/pkg/util"
|
|
)
|
|
|
|
// Defining the `Notice` business logic.
|
|
type Notice struct {
|
|
Trans *util.Trans
|
|
NoticeDAL *dal.Notice
|
|
}
|
|
|
|
// Query notices from the data access object based on the provided parameters and options.
|
|
func (a *Notice) Query(ctx context.Context, params schema.NoticeQueryParam) (*schema.NoticeQueryResult, error) {
|
|
params.Pagination = true
|
|
|
|
result, err := a.NoticeDAL.Query(ctx, params, schema.NoticeQueryOptions{
|
|
QueryOptions: util.QueryOptions{
|
|
OrderFields: []util.OrderByParam{
|
|
{Field: "created_at", Direction: util.DESC},
|
|
},
|
|
},
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// Get the specified notice from the data access object.
|
|
func (a *Notice) Get(ctx context.Context, id string) (*schema.Notice, error) {
|
|
notice, err := a.NoticeDAL.Get(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
} else if notice == nil {
|
|
return nil, errors.NotFound("", "Notice not found")
|
|
}
|
|
return notice, nil
|
|
}
|
|
|
|
// Create a new notice in the data access object.
|
|
func (a *Notice) Create(ctx context.Context, formItem *schema.NoticeForm) (*schema.Notice, error) {
|
|
notice := &schema.Notice{}
|
|
|
|
if err := formItem.FillTo(notice); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err := a.Trans.Exec(ctx, func(ctx context.Context) error {
|
|
if err := a.NoticeDAL.Create(ctx, notice); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return notice, nil
|
|
}
|
|
|
|
// Update the specified notice in the data access object.
|
|
func (a *Notice) Update(ctx context.Context, id string, formItem *schema.NoticeForm) error {
|
|
notice, err := a.NoticeDAL.Get(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
} else if notice == nil {
|
|
return errors.NotFound("", "Notice not found")
|
|
}
|
|
|
|
if err := formItem.FillTo(notice); err != nil {
|
|
return err
|
|
}
|
|
notice.UpdatedAt = time.Now()
|
|
|
|
return a.Trans.Exec(ctx, func(ctx context.Context) error {
|
|
if err := a.NoticeDAL.Update(ctx, notice); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
// Delete the specified notice from the data access object.
|
|
func (a *Notice) Delete(ctx context.Context, id string) error {
|
|
exists, err := a.NoticeDAL.Exists(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
} else if !exists {
|
|
return errors.NotFound("", "Notice not found")
|
|
}
|
|
|
|
return a.Trans.Exec(ctx, func(ctx context.Context) error {
|
|
if err := a.NoticeDAL.Delete(ctx, id); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
})
|
|
}
|