refactor: transfer dto
This commit is contained in:
parent
d9a91b64bb
commit
b1ee438357
|
@ -2,6 +2,7 @@ package bill
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"github.com/samber/lo"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -20,26 +21,37 @@ func NewBillService(r BillRepository) BillService {
|
||||||
// - date is {year, month} when return a month bills
|
// - date is {year, month} when return a month bills
|
||||||
// - date is {year} when return a year bills
|
// - date is {year} when return a year bills
|
||||||
// - date is {} when return all bills
|
// - date is {} when return all bills
|
||||||
func (b *billService) GetBills(ctx context.Context, date BDate) ([]Bill, error) {
|
func (b *billService) GetBills(ctx context.Context, date BDate) ([]interface{}, error) {
|
||||||
if date.Year == 0 && (date.Month != 0 || date.Day != 0) {
|
if date.Year == 0 && (date.Month > 0 || date.Day > 0) {
|
||||||
date.Year = time.Now().Year()
|
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())
|
date.Month = int(time.Now().Month())
|
||||||
}
|
}
|
||||||
|
var bills []Bill
|
||||||
|
var err error
|
||||||
switch {
|
switch {
|
||||||
case date.Year == 0:
|
case date.Year == 0:
|
||||||
return b.billRepository.GetBills(ctx)
|
bills, err = b.billRepository.GetBills(ctx)
|
||||||
case date.Month == 0:
|
case date.Month == 0:
|
||||||
return b.billRepository.GetBillByYear(ctx, date.Year)
|
bills, err = b.billRepository.GetBillByYear(ctx, date.Year)
|
||||||
case date.Day == 0:
|
case date.Day == 0:
|
||||||
return b.billRepository.GetBillByMonth(ctx, date.Year, date.Month)
|
bills, err = b.billRepository.GetBillByMonth(ctx, date.Year, date.Month)
|
||||||
default:
|
default:
|
||||||
return b.billRepository.GetBillByDay(ctx, date.Year, date.Month, date.Day)
|
bills, err = b.billRepository.GetBillByDay(ctx, date.Year, date.Month, date.Day)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return lo.Map(bills, func(b Bill, _ int) interface{} {
|
||||||
|
return b.Dto()
|
||||||
|
}), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *billService) GetBillByID(ctx context.Context, id int) (*Bill, error) {
|
func (b *billService) GetBillByID(ctx context.Context, id int) (interface{}, error) {
|
||||||
return b.billRepository.GetBillByID(ctx, id)
|
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 {
|
func (b *billService) CreateBill(ctx context.Context, bill *Bill) error {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user