to: add sqlite

This commit is contained in:
Liangzhao Che 2023-06-01 17:55:41 +08:00
parent 50c3e72bf1
commit 5ea6779d75
4 changed files with 24 additions and 28 deletions

View File

@ -1,9 +1,13 @@
package bill package bill
import "context" import (
"context"
"gorm.io/gorm"
)
// Bill represents 'bills' object. // Bill represents 'bills' object.
type Bill struct { type Bill struct {
gorm.Model
ID int `json:"id"` ID int `json:"id"`
Type string `json:"type"` Type string `json:"type"`
Date string `json:"date"` Date string `json:"date"`

View File

@ -2,63 +2,55 @@ package bill
import ( import (
"context" "context"
"database/sql" "gorm.io/gorm"
) )
const ( type databaseRepository struct {
QUERY_GET_BILLS = "SELECT * FROM Bills" db *gorm.DB
QUERY_GET_BILLS_BY_DAY = ""
QUERY_GET_BILLS_BY_MONTH = ""
QUERY_GET_BILLS_BY_YEAR = ""
QUERY_GET_BILL_BY_ID = ""
)
type mariaDBRepository struct {
mariadb *sql.DB
} }
func NewMariaDBRepository(mariadb *sql.DB) BillRepository { func NewDatabaseRepository(db *gorm.DB) BillRepository {
return &mariaDBRepository{ return &databaseRepository{
mariadb: mariadb, db: db,
} }
} }
func (m *mariaDBRepository) GetBills(ctx context.Context) (*[]Bill, error) { func (db *databaseRepository) GetBills(ctx context.Context) (*[]Bill, error) {
//TODO implement me //TODO implement me
panic("implement me") panic("implement me")
} }
func (m *mariaDBRepository) GetBillByDay(ctx context.Context, year int, month int, day int) (*[]Bill, error) { func (db *databaseRepository) GetBillByDay(ctx context.Context, year int, month int, day int) (*[]Bill, error) {
//TODO implement me //TODO implement me
panic("implement me") panic("implement me")
} }
func (m *mariaDBRepository) GetBillByMonth(ctx context.Context, year int, month int) (*[]Bill, error) { func (db *databaseRepository) GetBillByMonth(ctx context.Context, year int, month int) (*[]Bill, error) {
//TODO implement me //TODO implement me
panic("implement me") panic("implement me")
} }
func (m *mariaDBRepository) GetBillByYear(ctx context.Context, year int) (*[]Bill, error) { func (db *databaseRepository) GetBillByYear(ctx context.Context, year int) (*[]Bill, error) {
//TODO implement me //TODO implement me
panic("implement me") panic("implement me")
} }
func (m *mariaDBRepository) GetBillByID(ctx context.Context, id int) (*Bill, error) { func (db *databaseRepository) GetBillByID(ctx context.Context, id int) (*Bill, error) {
//TODO implement me //TODO implement me
panic("implement me") panic("implement me")
} }
func (m *mariaDBRepository) CreateBill(ctx context.Context, bill *Bill) error { func (db *databaseRepository) CreateBill(ctx context.Context, bill *Bill) error {
//TODO implement me //TODO implement me
panic("implement me") panic("implement me")
} }
func (m *mariaDBRepository) UpdateBill(ctx context.Context, bill *Bill) error { func (db *databaseRepository) UpdateBill(ctx context.Context, bill *Bill) error {
//TODO implement me //TODO implement me
panic("implement me") panic("implement me")
} }
func (m *mariaDBRepository) DeleteBill(ctx context.Context, id int) error { func (db *databaseRepository) DeleteBill(ctx context.Context, id int) error {
//TODO implement me //TODO implement me
panic("implement me") panic("implement me")
} }

View File

@ -21,8 +21,8 @@ import (
// Run our Fiber webserver. // Run our Fiber webserver.
func Run() { func Run() {
// Try to connect to our database as the initial part. // Try to connect to our database as the initial part.
mariadb, err := ConnectToMariaDB() //mariadb, err := ConnectToMariaDB()
ConnectToSqlite() sqlite, err := ConnectToSqlite()
if err != nil { if err != nil {
log.Fatal("Database connection error: $s", err) log.Fatal("Database connection error: $s", err)
} }
@ -54,7 +54,7 @@ func Run() {
// Create repositories. // Create repositories.
//cityRepository := city.NewCityRepository(mariadb) //cityRepository := city.NewCityRepository(mariadb)
//userRepository := user.NewUserRepository(mariadb) //userRepository := user.NewUserRepository(mariadb)
billRepository := bill.NewMariaDBRepository(mariadb) billRepository := bill.NewDatabaseRepository(sqlite)
// Create all of our services. // Create all of our services.
//cityService := city.NewCityService(cityRepository) //cityService := city.NewCityService(cityRepository)

View File

@ -6,11 +6,11 @@ import (
) )
func ConnectToSqlite() (*gorm.DB, error) { func ConnectToSqlite() (*gorm.DB, error) {
dsn := "test.db" dsn := "bill.db"
//dsn := "file::memory:?cache=shared" //dsn := "file::memory:?cache=shared"
db, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{}) db, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{})
if err != nil { if err != nil {
panic("failed to connect database") panic("failed to connect database.\n")
} }
return db, nil return db, nil