forked from pagu-project/pagu
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from akbariandev/ref/repository
refactor(repository): add entity and seperate repository package from…
- Loading branch information
Showing
12 changed files
with
154 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
internal/repository/faucet/faucet.go → internal/entity/faucet.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package faucet | ||
package entity | ||
|
||
import "gorm.io/gorm" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package zealy | ||
package entity | ||
|
||
import "gorm.io/gorm" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |