Skip to content

Commit

Permalink
Merge pull request #8 from akbariandev/ref/repository
Browse files Browse the repository at this point in the history
refactor(repository): add entity and seperate repository package from…
  • Loading branch information
akbariandev authored Jun 10, 2024
2 parents 217a320 + 5a7c88d commit 71d8821
Show file tree
Hide file tree
Showing 12 changed files with 154 additions and 140 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@ jobs:
with:
go-version: 1.21.1

- name: Vendor Go Modules
run: go mod vendor

- name: Build the binary
run: make build
4 changes: 4 additions & 0 deletions .github/workflows/linting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ jobs:
with:
go-version: '1.21'
cache: false

- name: Vendor Go Modules
run: go mod vendor

- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ jobs:
with:
go-version: 1.21

- name: Vendor Go Modules
run: go mod vendor

- name: Unit tests
run: make unit_test

Expand Down
7 changes: 3 additions & 4 deletions internal/engine/command/phoenix/faucet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package phoenix

import (
"github.com/pagu-project/Pagu/internal/engine/command"
"github.com/pagu-project/Pagu/internal/repository/faucet"
"github.com/pagu-project/Pagu/internal/repository/user"
"github.com/pagu-project/Pagu/internal/entity"
)

func (pt *Phoenix) faucetHandler(cmd command.Command, _ command.AppID, callerID string, args ...string) command.CommandResult {
if !pt.db.HasUser(callerID) {
if err := pt.db.AddUser(
&user.User{
&entity.User{
ID: callerID,
},
); err != nil {
Expand All @@ -31,7 +30,7 @@ func (pt *Phoenix) faucetHandler(cmd command.Command, _ command.AppID, callerID
return cmd.ErrorResult(err)
}

if err = pt.db.AddFaucet(&faucet.Faucet{
if err = pt.db.AddFaucet(&entity.Faucet{
Address: toAddr,
Amount: 5,
TransactionHash: txID,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package faucet
package entity

import "gorm.io/gorm"

Expand Down
6 changes: 2 additions & 4 deletions internal/repository/user/user.go → internal/entity/user.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package user
package entity

import (
"time"

"github.com/pagu-project/Pagu/internal/repository/faucet"
)

type User struct {
ID string `gorm:"primaryKey;unique"` // The ID that defined and assigned on Pagu.
Faucets []faucet.Faucet
Faucets []Faucet

CreatedAt time.Time
UpdatedAt time.Time
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package zealy
package entity

import "gorm.io/gorm"

Expand Down
134 changes: 7 additions & 127 deletions internal/repository/database.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package repository

import (
"time"

"github.com/pagu-project/Pagu/internal/repository/faucet"
"github.com/pagu-project/Pagu/internal/repository/user"
"github.com/pagu-project/Pagu/internal/repository/zealy"
"github.com/pagu-project/Pagu/internal/entity"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
Expand All @@ -22,13 +18,13 @@ func NewDB(path string) (*DB, error) {
}
}

if !db.Migrator().HasTable(&user.User{}) ||
!db.Migrator().HasTable(&faucet.Faucet{}) ||
!db.Migrator().HasTable(&zealy.ZealyUser{}) {
if !db.Migrator().HasTable(&entity.User{}) ||
!db.Migrator().HasTable(&entity.Faucet{}) ||
!db.Migrator().HasTable(&entity.ZealyUser{}) {
if err := db.AutoMigrate(
&user.User{},
&faucet.Faucet{},
&zealy.ZealyUser{},
&entity.User{},
&entity.Faucet{},
&entity.ZealyUser{},
); err != nil {
return nil, MigrationError{
Message: err.Error(),
Expand All @@ -40,119 +36,3 @@ func NewDB(path string) (*DB, error) {
DB: db,
}, nil
}

func (db *DB) AddUser(u *user.User) error {
tx := db.Create(u)
if tx.Error != nil {
return WriteError{
Message: tx.Error.Error(),
}
}

return nil
}

func (db *DB) GetUser(id string) (*user.User, error) {
var u *user.User
tx := db.Model(&user.User{}).Preload("Faucets").First(&u, "id = ?", id)
if tx.Error != nil {
return &user.User{}, ReadError{
Message: tx.Error.Error(),
}
}

return u, nil
}

func (db *DB) AddFaucet(f *faucet.Faucet) error {
tx := db.Create(f)
if tx.Error != nil {
return WriteError{
Message: tx.Error.Error(),
}
}

return nil
}

func (db *DB) HasUser(id string) bool {
var exists bool

_ = db.Model(&user.User{}).
Select("count(*) > 0").
Where("id = ?", id).
Find(&exists).
Error

return exists
}

func (db *DB) CanGetFaucet(id string) bool {
var u user.User
tx := db.Model(&user.User{}).Preload("Faucets").First(&u, "id = ?", id)
if tx.Error != nil {
return false
}

now := time.Now()

for _, f := range u.Faucets {
if f.CreatedAt.Year() == now.Year() &&
f.CreatedAt.Month() == now.Month() &&
f.CreatedAt.Day() == now.Day() {
return false
}
}

return true
}

//! Zealy Database

func (db *DB) GetZealyUser(id string) (*zealy.ZealyUser, error) {
var u *zealy.ZealyUser
tx := db.Model(&zealy.ZealyUser{}).First(&u, "discord_id = ?", id)
if tx.Error != nil {
return &zealy.ZealyUser{}, ReadError{
Message: tx.Error.Error(),
}
}

return u, nil
}

func (db *DB) AddZealyUser(u *zealy.ZealyUser) error {
tx := db.Create(u)
if tx.Error != nil {
return WriteError{
Message: tx.Error.Error(),
}
}

return nil
}

func (db *DB) UpdateZealyUser(id string, txHash string) error {
tx := db.Model(&zealy.ZealyUser{
DiscordID: id,
}).Where("discord_id = ?", id).Update("tx_hash", txHash)
if tx.Error != nil {
return WriteError{
Message: tx.Error.Error(),
}
}

return nil
}

func (db *DB) GetAllZealyUser() ([]*zealy.ZealyUser, error) {
var u []*zealy.ZealyUser
tx := db.Find(&u)
if tx.Error != nil {
return nil, ReadError{
Message: tx.Error.Error(),
}
}

return u, nil
}
6 changes: 3 additions & 3 deletions internal/repository/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func setup(t *testing.T) *DB {
func TestUserAndFaucet(t *testing.T) {
db := setup(t)
err := db.AddUser(&user.User{
err := db.AddUser(&entity.User{
ID: "123456789",
})
assert.NoError(t, err)
Expand All @@ -26,7 +26,7 @@ func TestUserAndFaucet(t *testing.T) {
r := db.CanGetFaucet("123456789")
assert.True(t, r)
err = db.AddFaucet(&faucet.Faucet{
err = db.AddFaucet(&entity.Faucet{
Address: "tpc1zlymfcuxlgvvuud2q4zw0scllqn74d2f90hld6w",
Amount: 5,
UserID: "123456789",
Expand All @@ -44,7 +44,7 @@ func TestUserAndFaucet(t *testing.T) {
func TestZealyDB(t *testing.T) {
db := setup(t)
err := db.AddZealyUser(&zealy.ZealyUser{
err := db.AddZealyUser(&entity.ZealyUser{
Amount: 100,
DiscordID: "12345678",
TxHash: "",
Expand Down
38 changes: 38 additions & 0 deletions internal/repository/faucet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package repository

import (
"time"

"github.com/pagu-project/Pagu/internal/entity"
)

func (db *DB) AddFaucet(f *entity.Faucet) error {
tx := db.Create(f)
if tx.Error != nil {
return WriteError{
Message: tx.Error.Error(),
}
}

return nil
}

func (db *DB) CanGetFaucet(id string) bool {
var u entity.User
tx := db.Model(&entity.User{}).Preload("Faucets").First(&u, "id = ?", id)
if tx.Error != nil {
return false
}

now := time.Now()

for _, f := range u.Faucets {
if f.CreatedAt.Year() == now.Year() &&
f.CreatedAt.Month() == now.Month() &&
f.CreatedAt.Day() == now.Day() {
return false
}
}

return true
}
38 changes: 38 additions & 0 deletions internal/repository/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package repository

import "github.com/pagu-project/Pagu/internal/entity"

func (db *DB) AddUser(u *entity.User) error {
tx := db.Create(u)
if tx.Error != nil {
return WriteError{
Message: tx.Error.Error(),
}
}

return nil
}

func (db *DB) GetUser(id string) (*entity.User, error) {
var u *entity.User
tx := db.Model(&entity.User{}).Preload("Faucets").First(&u, "id = ?", id)
if tx.Error != nil {
return &entity.User{}, ReadError{
Message: tx.Error.Error(),
}
}

return u, nil
}

func (db *DB) HasUser(id string) bool {
var exists bool

_ = db.Model(&entity.User{}).
Select("count(*) > 0").
Where("id = ?", id).
Find(&exists).
Error

return exists
}
51 changes: 51 additions & 0 deletions internal/repository/zealy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package repository

import "github.com/pagu-project/Pagu/internal/entity"

func (db *DB) GetZealyUser(id string) (*entity.ZealyUser, error) {
var u *entity.ZealyUser
tx := db.Model(&entity.ZealyUser{}).First(&u, "discord_id = ?", id)
if tx.Error != nil {
return &entity.ZealyUser{}, ReadError{
Message: tx.Error.Error(),
}
}

return u, nil
}

func (db *DB) AddZealyUser(u *entity.ZealyUser) error {
tx := db.Create(u)
if tx.Error != nil {
return WriteError{
Message: tx.Error.Error(),
}
}

return nil
}

func (db *DB) UpdateZealyUser(id string, txHash string) error {
tx := db.Model(&entity.ZealyUser{
DiscordID: id,
}).Where("discord_id = ?", id).Update("tx_hash", txHash)
if tx.Error != nil {
return WriteError{
Message: tx.Error.Error(),
}
}

return nil
}

func (db *DB) GetAllZealyUser() ([]*entity.ZealyUser, error) {
var u []*entity.ZealyUser
tx := db.Find(&u)
if tx.Error != nil {
return nil, ReadError{
Message: tx.Error.Error(),
}
}

return u, nil
}

0 comments on commit 71d8821

Please sign in to comment.