refactor: transfer dto

This commit is contained in:
clz 2023-06-10 16:53:44 +08:00
parent d9a91b64bb
commit b1ee438357

View File

@ -2,6 +2,7 @@ package bill
import (
"context"
"github.com/samber/lo"
"time"
)
@ -20,26 +21,37 @@ func NewBillService(r BillRepository) BillService {
// - date is {year, month} when return a month bills
// - date is {year} when return a year bills
// - date is {} when return all bills
func (b *billService) GetBills(ctx context.Context, date BDate) ([]Bill, error) {
if date.Year == 0 && (date.Month != 0 || date.Day != 0) {
func (b *billService) GetBills(ctx context.Context, date BDate) ([]interface{}, error) {
if date.Year == 0 && (date.Month > 0 || date.Day > 0) {
date.Year = time.Now().Year()
} else if date.Month == 0 && date.Day != 0 {
} else if date.Month == 0 && date.Day > 0 {
date.Month = int(time.Now().Month())
}
var bills []Bill
var err error
switch {
case date.Year == 0:
return b.billRepository.GetBills(ctx)
bills, err = b.billRepository.GetBills(ctx)
case date.Month == 0:
return b.billRepository.GetBillByYear(ctx, date.Year)
bills, err = b.billRepository.GetBillByYear(ctx, date.Year)
case date.Day == 0:
return b.billRepository.GetBillByMonth(ctx, date.Year, date.Month)
bills, err = b.billRepository.GetBillByMonth(ctx, date.Year, date.Month)
default:
return b.billRepository.GetBillByDay(ctx, date.Year, date.Month, date.Day)
}
bills, err = b.billRepository.GetBillByDay(ctx, date.Year, date.Month, date.Day)
}
func (b *billService) GetBillByID(ctx context.Context, id int) (*Bill, error) {
return b.billRepository.GetBillByID(ctx, id)
return lo.Map(bills, func(b Bill, _ int) interface{} {
return b.Dto()
}), err
}
func (b *billService) GetBillByID(ctx context.Context, id int) (interface{}, error) {
bill, err := b.billRepository.GetBillByID(ctx, id)
if err != nil {
return nil, err
}
billDto := bill.Dto()
return &billDto, nil
}
func (b *billService) CreateBill(ctx context.Context, bill *Bill) error {