bill-server-go/internal/bill/repository.go

69 lines
1.6 KiB
Go
Raw Normal View History

2023-05-08 21:48:34 +08:00
package bill
import (
"context"
2023-06-01 17:55:41 +08:00
"gorm.io/gorm"
2023-05-08 21:48:34 +08:00
)
2023-06-01 18:01:10 +08:00
type sqliteRepository struct {
2023-06-10 15:21:30 +08:00
*gorm.DB
2023-05-08 21:48:34 +08:00
}
2023-06-01 18:07:58 +08:00
func NewBillRepository(db *gorm.DB) BillRepository {
2023-06-01 18:01:10 +08:00
return &sqliteRepository{
2023-06-10 15:21:30 +08:00
db,
2023-05-08 21:48:34 +08:00
}
}
2023-06-10 15:21:30 +08:00
func (db *sqliteRepository) GetBills(ctx context.Context) ([]Bill, error) {
var bills []Bill
if err := db.Find(&bills).Error; err != nil {
return nil, err
}
return bills, nil
2023-05-08 21:48:34 +08:00
}
2023-06-10 15:21:30 +08:00
func (db *sqliteRepository) GetBillByDay(ctx context.Context, year int, month int, day int) ([]Bill, error) {
var bills []Bill
if err := db.Where("year =? AND month =? AND day =?", year, month, day).Find(&bills).Error; err != nil {
return nil, err
}
return bills, nil
2023-05-08 21:48:34 +08:00
}
2023-06-10 15:21:30 +08:00
func (db *sqliteRepository) GetBillByMonth(ctx context.Context, year int, month int) ([]Bill, error) {
var bills []Bill
if err := db.Where("year =? AND month =?", year, month).Find(&bills).Error; err != nil {
return nil, err
}
return bills, nil
2023-05-08 21:48:34 +08:00
}
2023-06-10 15:21:30 +08:00
func (db *sqliteRepository) GetBillByYear(ctx context.Context, year int) ([]Bill, error) {
var bills []Bill
if err := db.Where("year =?", year).Find(&bills).Error; err != nil {
return nil, err
}
return bills, nil
2023-05-08 21:48:34 +08:00
}
2023-06-01 18:01:10 +08:00
func (db *sqliteRepository) GetBillByID(ctx context.Context, id int) (*Bill, error) {
2023-06-10 15:21:30 +08:00
var bill Bill
if err := db.Where("id =?", id).Find(&bill).Error; err != nil {
return nil, err
}
return &bill, nil
2023-05-08 21:48:34 +08:00
}
2023-06-01 18:01:10 +08:00
func (db *sqliteRepository) CreateBill(ctx context.Context, bill *Bill) error {
2023-06-10 15:21:30 +08:00
return db.Create(bill).Error
2023-05-08 21:48:34 +08:00
}
2023-06-01 18:01:10 +08:00
func (db *sqliteRepository) UpdateBill(ctx context.Context, bill *Bill) error {
2023-06-10 15:21:30 +08:00
return db.Save(bill).Error
2023-05-08 21:48:34 +08:00
}
2023-06-01 18:01:10 +08:00
func (db *sqliteRepository) DeleteBill(ctx context.Context, id int) error {
2023-06-10 15:21:30 +08:00
return db.Where("id =?", id).Delete(&Bill{}).Error
2023-05-08 21:48:34 +08:00
}