database changes
This commit is contained in:
@@ -9,7 +9,7 @@ import (
|
|||||||
"go.mongodb.org/mongo-driver/v2/bson"
|
"go.mongodb.org/mongo-driver/v2/bson"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetGptChatHistory(db *laniakea.DatabaseContext, chatId string) ([]*AiChatMessage, error) {
|
func GetGptChatHistory(db *laniakea.DatabaseContext, chatId string) ([]AiChatMessage, error) {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
col := database.GetMongoCollection(db, "gpt_chat_messages")
|
col := database.GetMongoCollection(db, "gpt_chat_messages")
|
||||||
@@ -17,7 +17,7 @@ func GetGptChatHistory(db *laniakea.DatabaseContext, chatId string) ([]*AiChatMe
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
result := make([]*AiChatMessage, 0)
|
result := make([]AiChatMessage, 0)
|
||||||
err = cursor.All(ctx, &result)
|
err = cursor.All(ctx, &result)
|
||||||
return result, err
|
return result, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,12 +22,12 @@ type CodeEntry struct {
|
|||||||
TotalUsages uint `bson:"totalUsages"`
|
TotalUsages uint `bson:"totalUsages"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func FindCode(db *laniakea.DatabaseContext, code string) (*CodeEntry, error) {
|
func FindCode(db *laniakea.DatabaseContext, code string) (CodeEntry, error) {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
collection := database.GetMongoCollection(db, "codes")
|
collection := database.GetMongoCollection(db, "codes")
|
||||||
res := collection.FindOne(ctx, bson.M{"code": code})
|
res := collection.FindOne(ctx, bson.M{"code": code})
|
||||||
entry := new(CodeEntry)
|
entry := CodeEntry{}
|
||||||
err := res.Decode(entry)
|
err := res.Decode(entry)
|
||||||
return entry, err
|
return entry, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,14 +20,14 @@ type ConsoleLogEntry struct {
|
|||||||
TimeStamp int64 `bson:"timeStamp" json:"time_stamp"`
|
TimeStamp int64 `bson:"timeStamp" json:"time_stamp"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func WriteConsoleLog(db *laniakea.DatabaseContext, e *ConsoleLogEntry) error {
|
func WriteConsoleLog(db *laniakea.DatabaseContext, e ConsoleLogEntry) error {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
col := database.GetMongoCollection(db, "logs")
|
col := database.GetMongoCollection(db, "logs")
|
||||||
_, err := col.InsertOne(ctx, e)
|
_, err := col.InsertOne(ctx, e)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
func GetConsoleLogs(db *laniakea.DatabaseContext) (extypes.Slice[*ConsoleLogEntry], error) {
|
func GetConsoleLogs(db *laniakea.DatabaseContext) ([]ConsoleLogEntry, error) {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
col := database.GetMongoCollection(db, "logs")
|
col := database.GetMongoCollection(db, "logs")
|
||||||
@@ -39,7 +39,7 @@ func GetConsoleLogs(db *laniakea.DatabaseContext) (extypes.Slice[*ConsoleLogEntr
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer cursor.Close(ctx)
|
defer cursor.Close(ctx)
|
||||||
result := make(extypes.Slice[*ConsoleLogEntry], 0)
|
result := make([]ConsoleLogEntry, 0)
|
||||||
err = cursor.All(ctx, &result)
|
err = cursor.All(ctx, &result)
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ type AiChatMessage struct {
|
|||||||
Index int `bson:"index"`
|
Index int `bson:"index"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetRPChatHistory(db *laniakea.DatabaseContext, chatId string) ([]*AiChatMessage, error) {
|
func GetRPChatHistory(db *laniakea.DatabaseContext, chatId string) ([]AiChatMessage, error) {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
col := database.GetMongoCollection(db, "rp_chat_messages")
|
col := database.GetMongoCollection(db, "rp_chat_messages")
|
||||||
@@ -25,7 +25,7 @@ func GetRPChatHistory(db *laniakea.DatabaseContext, chatId string) ([]*AiChatMes
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
result := make([]*AiChatMessage, 0)
|
result := make([]AiChatMessage, 0)
|
||||||
err = cursor.All(ctx, &result)
|
err = cursor.All(ctx, &result)
|
||||||
return result, err
|
return result, err
|
||||||
}
|
}
|
||||||
@@ -47,7 +47,7 @@ func GetRPChatHistorySize(db *laniakea.DatabaseContext, chatId string) (int64, e
|
|||||||
col := database.GetMongoCollection(db, "rp_chat_messages")
|
col := database.GetMongoCollection(db, "rp_chat_messages")
|
||||||
return col.CountDocuments(ctx, bson.M{"chatId": chatId})
|
return col.CountDocuments(ctx, bson.M{"chatId": chatId})
|
||||||
}
|
}
|
||||||
func DeleteRPChatEntry(db *laniakea.DatabaseContext, entry *AiChatMessage) error {
|
func DeleteRPChatEntry(db *laniakea.DatabaseContext, entry AiChatMessage) error {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
col := database.GetMongoCollection(db, "rp_chat_messages")
|
col := database.GetMongoCollection(db, "rp_chat_messages")
|
||||||
|
|||||||
@@ -16,20 +16,20 @@ type AIRepository struct {
|
|||||||
db *sqlx.DB
|
db *sqlx.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func newAiRepository(db *sqlx.DB) *AIRepository {
|
func newAiRepository(db *sqlx.DB) AIRepository {
|
||||||
return &AIRepository{db}
|
return AIRepository{db}
|
||||||
}
|
}
|
||||||
func NewAIRepository(db *laniakea.DatabaseContext) *AIRepository {
|
func NewAIRepository(db *laniakea.DatabaseContext) AIRepository {
|
||||||
return newAiRepository(db.PostgresSQL)
|
return newAiRepository(db.PostgresSQL)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rep *AIRepository) GetModel(id string) (*AIModel, error) {
|
func (rep AIRepository) GetModel(id string) (AIModel, error) {
|
||||||
model := new(AIModel)
|
model := AIModel{}
|
||||||
err := rep.db.Get(model, "SELECT * FROM ai_models WHERE id=$1;", id)
|
err := rep.db.Get(&model, "SELECT * FROM ai_models WHERE id=$1;", id)
|
||||||
return model, err
|
return model, err
|
||||||
}
|
}
|
||||||
func (rep *AIRepository) GetAllModels() ([]*AIModel, error) {
|
func (rep AIRepository) GetAllModels() ([]AIModel, error) {
|
||||||
models := make([]*AIModel, 0)
|
models := make([]AIModel, 0)
|
||||||
err := rep.db.Select(&models, "SELECT * FROM ai_models ORDER BY id;")
|
err := rep.db.Select(&models, "SELECT * FROM ai_models ORDER BY id;")
|
||||||
return models, err
|
return models, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package psql
|
package psql
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"git.nix13.pw/scuroneko/laniakea"
|
||||||
"github.com/shopspring/decimal"
|
"github.com/shopspring/decimal"
|
||||||
"github.com/vinovest/sqlx"
|
"github.com/vinovest/sqlx"
|
||||||
)
|
)
|
||||||
@@ -18,17 +19,20 @@ type FractionRepository struct {
|
|||||||
db *sqlx.DB
|
db *sqlx.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFractionRepository(db *sqlx.DB) *FractionRepository {
|
func newFractionRepository(db *sqlx.DB) FractionRepository {
|
||||||
return &FractionRepository{db: db}
|
return FractionRepository{db}
|
||||||
|
}
|
||||||
|
func NewFractionRepository(db *laniakea.DatabaseContext) FractionRepository {
|
||||||
|
return newFractionRepository(db.PostgresSQL)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rep *FractionRepository) GetAll() ([]*Fraction, error) {
|
func (rep FractionRepository) GetAll() ([]Fraction, error) {
|
||||||
fractions := make([]*Fraction, 0)
|
fractions := make([]Fraction, 0)
|
||||||
err := rep.db.Select(&fractions, "SELECT * FROM fractions ORDER BY id DESC;")
|
err := rep.db.Select(&fractions, "SELECT * FROM fractions ORDER BY id DESC;")
|
||||||
return fractions, err
|
return fractions, err
|
||||||
}
|
}
|
||||||
func (rep *FractionRepository) GetById(id int32) (*Fraction, error) {
|
func (rep FractionRepository) GetById(id int32) (Fraction, error) {
|
||||||
fraction := new(Fraction)
|
fraction := Fraction{}
|
||||||
err := rep.db.Get(fraction, "SELECT * FROM fractions WHERE id = $1", id)
|
err := rep.db.Get(&fraction, "SELECT * FROM fractions WHERE id = $1", id)
|
||||||
return fraction, err
|
return fraction, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package psql
|
package psql
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"git.nix13.pw/scuroneko/laniakea"
|
||||||
"github.com/shopspring/decimal"
|
"github.com/shopspring/decimal"
|
||||||
"github.com/vinovest/sqlx"
|
"github.com/vinovest/sqlx"
|
||||||
)
|
)
|
||||||
@@ -21,15 +22,19 @@ type GroupRepository struct {
|
|||||||
db *sqlx.DB
|
db *sqlx.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewGroupRepository(db *sqlx.DB) *GroupRepository {
|
func newGroupRepository(db *sqlx.DB) GroupRepository {
|
||||||
return &GroupRepository{db: db}
|
return GroupRepository{db}
|
||||||
}
|
}
|
||||||
func (rep *GroupRepository) GetAll() ([]*Group, error) {
|
func NewGroupRepository(db *laniakea.DatabaseContext) GroupRepository {
|
||||||
groups := make([]*Group, 0)
|
return newGroupRepository(db.PostgresSQL)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rep GroupRepository) GetAll() ([]Group, error) {
|
||||||
|
groups := make([]Group, 0)
|
||||||
err := rep.db.Select(&groups, "SELECT * FROM groups ORDER BY id DESC;")
|
err := rep.db.Select(&groups, "SELECT * FROM groups ORDER BY id DESC;")
|
||||||
return groups, err
|
return groups, err
|
||||||
}
|
}
|
||||||
func (rep *GroupRepository) GetById(id int) (*Group, error) {
|
func (rep GroupRepository) GetById(id int) (Group, error) {
|
||||||
group, err := sqlx.One[Group](rep.db, "SELECT * FROM groups WHERE id = $1", id)
|
group, err := sqlx.One[Group](rep.db, "SELECT * FROM groups WHERE id = $1", id)
|
||||||
return &group, err
|
return group, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,46 +45,57 @@ type RPRepository struct {
|
|||||||
db *sqlx.DB
|
db *sqlx.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRPRepository(db *laniakea.DatabaseContext) *RPRepository {
|
func newRpRepository(db *sqlx.DB) RPRepository {
|
||||||
return &RPRepository{db.PostgresSQL}
|
return RPRepository{db}
|
||||||
|
}
|
||||||
|
func NewRPRepository(db *laniakea.DatabaseContext) RPRepository {
|
||||||
|
return newRpRepository(db.PostgresSQL)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rep *RPRepository) GetOrCreateUser(id int64) (*RPUser, error) {
|
func (rep RPRepository) GetOrCreateUser(id int64) (RPUser, error) {
|
||||||
user, err := rep.GetUser(id)
|
user, err := rep.GetUser(id)
|
||||||
if errors.Is(err, sql.ErrNoRows) {
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
return rep.CreateUser(id)
|
return rep.CreateUser(id)
|
||||||
}
|
}
|
||||||
return user, err
|
return user, err
|
||||||
}
|
}
|
||||||
func (rep *RPRepository) CreateUser(id int64) (*RPUser, error) {
|
func (rep RPRepository) CreateUser(id int64) (RPUser, error) {
|
||||||
user := new(RPUser)
|
user := RPUser{}
|
||||||
err := rep.db.Get(user, "INSERT INTO rp_users(user_id) VALUES ($1) RETURNING *;", id)
|
err := rep.db.Get(&user, "INSERT INTO rp_users(user_id) VALUES ($1) RETURNING *;", id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return user, err
|
return user, err
|
||||||
}
|
}
|
||||||
user.Preset, err = rep.GetPreset(user.SelectedPreset)
|
preset, err := rep.GetPreset(user.SelectedPreset)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return user, err
|
return user, err
|
||||||
}
|
}
|
||||||
aiRep := newAiRepository(rep.db)
|
aiRep := newAiRepository(rep.db)
|
||||||
user.Model, err = aiRep.GetModel(user.SelectedModel)
|
model, err := aiRep.GetModel(user.SelectedModel)
|
||||||
return user, err
|
|
||||||
}
|
|
||||||
func (rep *RPRepository) GetUser(id int64) (*RPUser, error) {
|
|
||||||
user := new(RPUser)
|
|
||||||
err := rep.db.Get(user, "SELECT * FROM rp_users WHERE user_id=$1", id)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return user, err
|
return user, err
|
||||||
}
|
}
|
||||||
user.Preset, err = rep.GetPreset(user.SelectedPreset)
|
|
||||||
|
user.Preset = &preset
|
||||||
|
user.Model = &model
|
||||||
|
return user, err
|
||||||
|
}
|
||||||
|
func (rep RPRepository) GetUser(id int64) (RPUser, error) {
|
||||||
|
user := RPUser{}
|
||||||
|
err := rep.db.Get(&user, "SELECT * FROM rp_users WHERE user_id=$1", id)
|
||||||
|
if err != nil {
|
||||||
|
return user, err
|
||||||
|
}
|
||||||
|
preset, err := rep.GetPreset(user.SelectedPreset)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return user, err
|
return user, err
|
||||||
}
|
}
|
||||||
aiRep := newAiRepository(rep.db)
|
aiRep := newAiRepository(rep.db)
|
||||||
user.Model, err = aiRep.GetModel(user.SelectedModel)
|
model, err := aiRep.GetModel(user.SelectedModel)
|
||||||
|
user.Model = &model
|
||||||
|
user.Preset = &preset
|
||||||
return user, err
|
return user, err
|
||||||
}
|
}
|
||||||
func (rep *RPRepository) UpdateUser(user *RPUser) error {
|
func (rep RPRepository) UpdateUser(user RPUser) error {
|
||||||
s, args, err := sqlx.In(
|
s, args, err := sqlx.In(
|
||||||
"UPDATE rp_users SET selected_preset=?, used_tokens=?, user_prompt=?, selected_model=? WHERE user_id=?;",
|
"UPDATE rp_users SET selected_preset=?, used_tokens=?, user_prompt=?, selected_model=? WHERE user_id=?;",
|
||||||
user.SelectedPreset, user.UsedTokens, user.UserPrompt, user.SelectedModel, user.UserID,
|
user.SelectedPreset, user.UsedTokens, user.UserPrompt, user.SelectedModel, user.UserID,
|
||||||
@@ -96,7 +107,7 @@ func (rep *RPRepository) UpdateUser(user *RPUser) error {
|
|||||||
_, err = rep.db.Exec(s, args...)
|
_, err = rep.db.Exec(s, args...)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
func (rep *RPRepository) UpdateUserPreset(user *RPUser, presetId string) (*RPPreset, error) {
|
func (rep RPRepository) UpdateUserPreset(user RPUser, presetId string) (RPPreset, error) {
|
||||||
preset, err := rep.GetPreset(presetId)
|
preset, err := rep.GetPreset(presetId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return preset, err
|
return preset, err
|
||||||
@@ -112,7 +123,7 @@ func (rep *RPRepository) UpdateUserPreset(user *RPUser, presetId string) (*RPPre
|
|||||||
_, err = rep.db.Exec(s, args...)
|
_, err = rep.db.Exec(s, args...)
|
||||||
return preset, err
|
return preset, err
|
||||||
}
|
}
|
||||||
func (rep *RPRepository) GetUserPreset(user *RPUser) (*RPPreset, error) {
|
func (rep RPRepository) GetUserPreset(user RPUser) (RPPreset, error) {
|
||||||
preset, err := rep.GetPreset(user.SelectedPreset)
|
preset, err := rep.GetPreset(user.SelectedPreset)
|
||||||
if errors.Is(err, sql.ErrNoRows) {
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
return rep.UpdateUserPreset(user, "soft")
|
return rep.UpdateUserPreset(user, "soft")
|
||||||
@@ -120,40 +131,40 @@ func (rep *RPRepository) GetUserPreset(user *RPUser) (*RPPreset, error) {
|
|||||||
return preset, err
|
return preset, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rep *RPRepository) GetAllPresets() ([]*RPPreset, error) {
|
func (rep RPRepository) GetAllPresets() ([]RPPreset, error) {
|
||||||
presets := make([]*RPPreset, 0)
|
presets := make([]RPPreset, 0)
|
||||||
err := rep.db.Select(&presets, "SELECT * FROM rp_presets ORDER BY id;")
|
err := rep.db.Select(&presets, "SELECT * FROM rp_presets ORDER BY id;")
|
||||||
return presets, err
|
return presets, err
|
||||||
}
|
}
|
||||||
func (rep *RPRepository) GetPreset(id string) (*RPPreset, error) {
|
func (rep RPRepository) GetPreset(id string) (RPPreset, error) {
|
||||||
preset := new(RPPreset)
|
preset := RPPreset{}
|
||||||
err := rep.db.Get(preset, "SELECT * FROM rp_presets WHERE id=$1;", id)
|
err := rep.db.Get(&preset, "SELECT * FROM rp_presets WHERE id=$1;", id)
|
||||||
return preset, err
|
return preset, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rep *RPRepository) GetAllScenarios() ([]*RPScenario, error) {
|
func (rep RPRepository) GetAllScenarios() ([]RPScenario, error) {
|
||||||
scenarios := make([]*RPScenario, 0)
|
scenarios := make([]RPScenario, 0)
|
||||||
err := rep.db.Select(&scenarios, "SELECT * FROM rp_scenarios ORDER BY id;")
|
err := rep.db.Select(&scenarios, "SELECT * FROM rp_scenarios ORDER BY id;")
|
||||||
return scenarios, err
|
return scenarios, err
|
||||||
}
|
}
|
||||||
func (rep *RPRepository) GetScenario(id int) (*RPScenario, error) {
|
func (rep RPRepository) GetScenario(id int) (RPScenario, error) {
|
||||||
scenario := new(RPScenario)
|
scenario := RPScenario{}
|
||||||
err := rep.db.Get(scenario, "SELECT * FROM rp_scenarios WHERE id=$1;", id)
|
err := rep.db.Get(&scenario, "SELECT * FROM rp_scenarios WHERE id=$1;", id)
|
||||||
return scenario, err
|
return scenario, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rep *RPRepository) GetAllSettings() ([]*RPSetting, error) {
|
func (rep RPRepository) GetAllSettings() ([]RPSetting, error) {
|
||||||
settings := make([]*RPSetting, 0)
|
settings := make([]RPSetting, 0)
|
||||||
err := rep.db.Select(&settings, "SELECT * FROM rp_settings ORDER BY id;")
|
err := rep.db.Select(&settings, "SELECT * FROM rp_settings ORDER BY id;")
|
||||||
return settings, err
|
return settings, err
|
||||||
}
|
}
|
||||||
func (rep *RPRepository) GetSetting(id int) (*RPSetting, error) {
|
func (rep RPRepository) GetSetting(id int) (RPSetting, error) {
|
||||||
setting := new(RPSetting)
|
setting := RPSetting{}
|
||||||
err := rep.db.Get(setting, "SELECT * FROM rp_settings WHERE id=$1;", id)
|
err := rep.db.Get(&setting, "SELECT * FROM rp_settings WHERE id=$1;", id)
|
||||||
return setting, err
|
return setting, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rep *RPRepository) UpdateUserCompressSettings(user *RPUser) (*RPUser, error) {
|
func (rep RPRepository) UpdateUserCompressSettings(user RPUser) (RPUser, error) {
|
||||||
query, args, err := sqlx.In(
|
query, args, err := sqlx.In(
|
||||||
"UPDATE rp_users SET compress_method=?, compress_limit=? WHERE user_id=?;",
|
"UPDATE rp_users SET compress_method=?, compress_limit=? WHERE user_id=?;",
|
||||||
user.CompressMethod, user.CompressLimit, user.UserID,
|
user.CompressMethod, user.CompressLimit, user.UserID,
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package psql
|
package psql
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"git.nix13.pw/scuroneko/laniakea"
|
||||||
"github.com/shopspring/decimal"
|
"github.com/shopspring/decimal"
|
||||||
"github.com/vinovest/sqlx"
|
"github.com/vinovest/sqlx"
|
||||||
)
|
)
|
||||||
@@ -36,47 +37,48 @@ type ShopRepository struct {
|
|||||||
db *sqlx.DB
|
db *sqlx.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewShopRepository(db *sqlx.DB) *ShopRepository {
|
func NewShopRepository(db *laniakea.DatabaseContext) ShopRepository {
|
||||||
return &ShopRepository{db: db}
|
return newShopRepository(db.PostgresSQL)
|
||||||
}
|
}
|
||||||
|
func newShopRepository(db *sqlx.DB) ShopRepository { return ShopRepository{db} }
|
||||||
|
|
||||||
func (rep *ShopRepository) GetAllAuto() ([]*ShopAuto, error) {
|
func (rep ShopRepository) GetAllAuto() ([]ShopAuto, error) {
|
||||||
auto := make([]*ShopAuto, 0)
|
auto := make([]ShopAuto, 0)
|
||||||
err := rep.db.Select(&auto, "SELECT * FROM shop_auto ORDER BY id DESC;")
|
err := rep.db.Select(&auto, "SELECT * FROM shop_auto ORDER BY id DESC;")
|
||||||
return auto, err
|
return auto, err
|
||||||
}
|
}
|
||||||
func (rep *ShopRepository) GetAuto(id int32) (*ShopAuto, error) {
|
func (rep ShopRepository) GetAuto(id int32) (ShopAuto, error) {
|
||||||
auto := new(ShopAuto)
|
auto := ShopAuto{}
|
||||||
err := rep.db.Get(auto, "SELECT * FROM shop_auto WHERE id = $1;", id)
|
err := rep.db.Get(&auto, "SELECT * FROM shop_auto WHERE id = $1;", id)
|
||||||
return auto, err
|
return auto, err
|
||||||
}
|
}
|
||||||
func (rep *ShopRepository) GetAllBusinesses() ([]*ShopBusiness, error) {
|
func (rep ShopRepository) GetAllBusinesses() ([]ShopBusiness, error) {
|
||||||
businesses := make([]*ShopBusiness, 0)
|
businesses := make([]ShopBusiness, 0)
|
||||||
err := rep.db.Select(&businesses, "SELECT * FROM shop_business ORDER BY id DESC;")
|
err := rep.db.Select(&businesses, "SELECT * FROM shop_business ORDER BY id DESC;")
|
||||||
return businesses, err
|
return businesses, err
|
||||||
}
|
}
|
||||||
func (rep *ShopRepository) GetBusiness(id int32) (*ShopBusiness, error) {
|
func (rep ShopRepository) GetBusiness(id int32) (ShopBusiness, error) {
|
||||||
business := new(ShopBusiness)
|
business := ShopBusiness{}
|
||||||
err := rep.db.Get(business, "SELECT * FROM shop_business WHERE id = $1;", id)
|
err := rep.db.Get(&business, "SELECT * FROM shop_business WHERE id = $1;", id)
|
||||||
return business, err
|
return business, err
|
||||||
}
|
}
|
||||||
func (rep *ShopRepository) GetAllMaids() ([]*ShopMaid, error) {
|
func (rep ShopRepository) GetAllMaids() ([]ShopMaid, error) {
|
||||||
maids := make([]*ShopMaid, 0)
|
maids := make([]ShopMaid, 0)
|
||||||
err := rep.db.Select(&maids, "SELECT * FROM shop_maid ORDER BY id DESC;")
|
err := rep.db.Select(&maids, "SELECT * FROM shop_maid ORDER BY id DESC;")
|
||||||
return maids, err
|
return maids, err
|
||||||
}
|
}
|
||||||
func (rep *ShopRepository) GetMaid(id int32) (*ShopMaid, error) {
|
func (rep ShopRepository) GetMaid(id int32) (ShopMaid, error) {
|
||||||
maid := new(ShopMaid)
|
maid := ShopMaid{}
|
||||||
err := rep.db.Get(maid, "SELECT * FROM shop_maid WHERE id = $1;", id)
|
err := rep.db.Get(&maid, "SELECT * FROM shop_maid WHERE id = $1;", id)
|
||||||
return maid, err
|
return maid, err
|
||||||
}
|
}
|
||||||
func (rep *ShopRepository) GetAllMiners() ([]*ShopMiner, error) {
|
func (rep ShopRepository) GetAllMiners() ([]ShopMiner, error) {
|
||||||
miners := make([]*ShopMiner, 0)
|
miners := make([]ShopMiner, 0)
|
||||||
err := rep.db.Select(&miners, "SELECT * FROM shop_miner ORDER BY id DESC;")
|
err := rep.db.Select(&miners, "SELECT * FROM shop_miner ORDER BY id DESC;")
|
||||||
return miners, err
|
return miners, err
|
||||||
}
|
}
|
||||||
func (rep *ShopRepository) GetMiner(id int32) (*ShopMiner, error) {
|
func (rep ShopRepository) GetMiner(id int32) (ShopMiner, error) {
|
||||||
miner := new(ShopMiner)
|
miner := ShopMiner{}
|
||||||
err := rep.db.Get(miner, "SELECT * FROM shop_miner WHERE id = $1;", id)
|
err := rep.db.Get(&miner, "SELECT * FROM shop_miner WHERE id = $1;", id)
|
||||||
return miner, err
|
return miner, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -55,11 +55,12 @@ type UserRepository struct {
|
|||||||
db *sqlx.DB
|
db *sqlx.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUserRepository(db *laniakea.DatabaseContext) *UserRepository {
|
func newUserRepository(db *sqlx.DB) UserRepository { return UserRepository{db} }
|
||||||
return &UserRepository{db: db.PostgresSQL}
|
func NewUserRepository(db *laniakea.DatabaseContext) UserRepository {
|
||||||
|
return newUserRepository(db.PostgresSQL)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rep *UserRepository) GetOrCreate(tgId int, name string) (*User, error) {
|
func (rep UserRepository) GetOrCreate(tgId int, name string) (*User, error) {
|
||||||
user, err := rep.GetById(tgId)
|
user, err := rep.GetById(tgId)
|
||||||
if errors.Is(err, sql.ErrNoRows) {
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
user, err = rep.Create(tgId, name)
|
user, err = rep.Create(tgId, name)
|
||||||
@@ -67,73 +68,22 @@ func (rep *UserRepository) GetOrCreate(tgId int, name string) (*User, error) {
|
|||||||
return user, err
|
return user, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rep *UserRepository) Create(id int, name string) (*User, error) {
|
func (rep UserRepository) Create(id int, name string) (*User, error) {
|
||||||
user := new(User)
|
user := User{}
|
||||||
err := rep.db.Get(user, "INSERT INTO users (id, name) VALUES ($1, $2) RETURNING *;", id, name)
|
err := rep.db.Get(user, "INSERT INTO users (id, name) VALUES ($1, $2) RETURNING *;", id, name)
|
||||||
return user, err
|
return &user, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rep *UserRepository) GetById(telegramId int) (*User, error) {
|
func (rep UserRepository) GetById(telegramId int) (*User, error) {
|
||||||
user := new(User)
|
user := User{}
|
||||||
err := sqlx.Transact(rep.db, func(ctx context.Context, db sqlx.Queryable) error {
|
err := rep.db.Get(&user, "SELECT * FROM users WHERE id=$1;", telegramId)
|
||||||
err := rep.db.Get(user, "SELECT * FROM users WHERE id=$1;", telegramId)
|
if err != nil {
|
||||||
if err != nil {
|
return &user, err
|
||||||
return err
|
}
|
||||||
}
|
return rep.GetJoins(&user)
|
||||||
user.Group = new(Group)
|
|
||||||
err = rep.db.Get(user.Group, "SELECT * FROM groups WHERE id=$1;", user.GroupID)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
user.Work = new(Work)
|
|
||||||
err = rep.db.Get(user.Work, "SELECT * FROM works WHERE id=$1;", user.WorkID)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
shopRep := NewShopRepository(rep.db)
|
|
||||||
if user.AutoID.Valid {
|
|
||||||
user.Auto, err = shopRep.GetAuto(user.AutoID.Int32)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if user.BusinessID.Valid {
|
|
||||||
user.Business, err = shopRep.GetBusiness(user.BusinessID.Int32)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if user.MaidID.Valid {
|
|
||||||
user.Maid, err = shopRep.GetMaid(user.MaidID.Int32)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if user.MinerID.Valid {
|
|
||||||
user.Miner, err = shopRep.GetMiner(user.MinerID.Int32)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if user.PairID.Valid {
|
|
||||||
user.Pair, err = rep.GetById(int(user.PairID.Int64))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if user.FractionID.Valid {
|
|
||||||
fractionRep := NewFractionRepository(rep.db)
|
|
||||||
user.Fraction, err = fractionRep.GetById(user.FractionID.Int32)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
return user, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rep *UserRepository) GetJoins(user *User) (*User, error) {
|
func (rep UserRepository) GetJoins(user *User) (*User, error) {
|
||||||
err := sqlx.Transact(rep.db, func(ctx context.Context, db sqlx.Queryable) error {
|
err := sqlx.Transact(rep.db, func(ctx context.Context, db sqlx.Queryable) error {
|
||||||
user.Group = new(Group)
|
user.Group = new(Group)
|
||||||
err := rep.db.Get(user.Group, "SELECT * FROM groups WHERE id=$1;", user.GroupID)
|
err := rep.db.Get(user.Group, "SELECT * FROM groups WHERE id=$1;", user.GroupID)
|
||||||
@@ -145,50 +95,56 @@ func (rep *UserRepository) GetJoins(user *User) (*User, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
shopRep := NewShopRepository(rep.db)
|
shopRep := newShopRepository(rep.db)
|
||||||
if user.AutoID.Valid {
|
if user.AutoID.Valid {
|
||||||
user.Auto, err = shopRep.GetAuto(user.AutoID.Int32)
|
auto, err := shopRep.GetAuto(user.AutoID.Int32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
user.Auto = &auto
|
||||||
}
|
}
|
||||||
if user.BusinessID.Valid {
|
if user.BusinessID.Valid {
|
||||||
user.Business, err = shopRep.GetBusiness(user.BusinessID.Int32)
|
business, err := shopRep.GetBusiness(user.BusinessID.Int32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
user.Business = &business
|
||||||
}
|
}
|
||||||
if user.MaidID.Valid {
|
if user.MaidID.Valid {
|
||||||
user.Maid, err = shopRep.GetMaid(user.MaidID.Int32)
|
maid, err := shopRep.GetMaid(user.MaidID.Int32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
user.Maid = &maid
|
||||||
}
|
}
|
||||||
if user.MinerID.Valid {
|
if user.MinerID.Valid {
|
||||||
user.Miner, err = shopRep.GetMiner(user.MinerID.Int32)
|
miner, err := shopRep.GetMiner(user.MinerID.Int32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
user.Miner = &miner
|
||||||
}
|
}
|
||||||
if user.PairID.Valid {
|
if user.PairID.Valid {
|
||||||
user.Pair, err = rep.GetById(int(user.PairID.Int64))
|
pair, err := rep.GetById(int(user.PairID.Int64))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
user.Pair = pair
|
||||||
}
|
}
|
||||||
if user.FractionID.Valid {
|
if user.FractionID.Valid {
|
||||||
fractionRep := NewFractionRepository(rep.db)
|
fractionRep := newFractionRepository(rep.db)
|
||||||
user.Fraction, err = fractionRep.GetById(user.FractionID.Int32)
|
fraction, err := fractionRep.GetById(user.FractionID.Int32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
user.Fraction = &fraction
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
return user, err
|
return user, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rep *UserRepository) Update(user *User) (*User, error) {
|
func (rep UserRepository) Update(user *User) (*User, error) {
|
||||||
_, err := rep.db.NamedExec(
|
_, err := rep.db.NamedExec(
|
||||||
`UPDATE users SET balance=:balance, name=:name, group_id=:group_id, level=:level, exp=:exp,
|
`UPDATE users SET balance=:balance, name=:name, group_id=:group_id, level=:level, exp=:exp,
|
||||||
work_id=:work_id, work_time=:work_time, auto_id=:auto_id, business_id=:business_id,
|
work_id=:work_id, work_time=:work_time, auto_id=:auto_id, business_id=:business_id,
|
||||||
@@ -205,20 +161,20 @@ func (rep *UserRepository) Update(user *User) (*User, error) {
|
|||||||
return rep.GetById(user.ID)
|
return rep.GetById(user.ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rep *UserRepository) GetAll() ([]*User, error) {
|
func (rep UserRepository) GetAll() ([]User, error) {
|
||||||
users := make([]*User, 0)
|
users := make([]User, 0)
|
||||||
err := rep.db.Select(&users, "SELECT * FROM users ORDER BY id;")
|
err := rep.db.Select(&users, "SELECT * FROM users ORDER BY id;")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
outUsers := make([]*User, len(users))
|
outUsers := make([]User, len(users))
|
||||||
for i, user := range users {
|
for i, user := range users {
|
||||||
u, err := rep.GetJoins(user)
|
u, err := rep.GetJoins(&user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
outUsers[i] = u
|
outUsers[i] = *u
|
||||||
}
|
}
|
||||||
|
|
||||||
return outUsers, nil
|
return outUsers, nil
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package psql
|
|||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
|
||||||
"git.nix13.pw/scuroneko/extypes"
|
|
||||||
"git.nix13.pw/scuroneko/laniakea"
|
"git.nix13.pw/scuroneko/laniakea"
|
||||||
"github.com/shopspring/decimal"
|
"github.com/shopspring/decimal"
|
||||||
"github.com/vinovest/sqlx"
|
"github.com/vinovest/sqlx"
|
||||||
@@ -31,11 +30,9 @@ func NewWaifuRepository(db *laniakea.DatabaseContext) *WaifuRepository {
|
|||||||
return &WaifuRepository{db: db.PostgresSQL}
|
return &WaifuRepository{db: db.PostgresSQL}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rep *WaifuRepository) GetAll() (extypes.Slice[*Waifu], error) {
|
func (rep *WaifuRepository) GetAll() ([]Waifu, error) {
|
||||||
waifus, err := sqlx.List[*Waifu](rep.db, "SELECT waifus.* FROM waifus;")
|
waifus, err := sqlx.List[Waifu](rep.db, "SELECT * FROM waifus;")
|
||||||
if err != nil {
|
//userRep := newUserRepository(rep.db)
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, waifu := range waifus {
|
for _, waifu := range waifus {
|
||||||
if !waifu.OwnerID.Valid {
|
if !waifu.OwnerID.Valid {
|
||||||
@@ -47,18 +44,20 @@ func (rep *WaifuRepository) GetAll() (extypes.Slice[*Waifu], error) {
|
|||||||
return waifus, err
|
return waifus, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rep *WaifuRepository) GetByUserId(userId int) ([]*Waifu, error) {
|
func (rep *WaifuRepository) GetByUserId(userId int) ([]Waifu, error) {
|
||||||
waifus, err := sqlx.List[*Waifu](rep.db, "SELECT waifus.* FROM waifus WHERE owner_id=$1;", userId)
|
waifus, err := sqlx.List[Waifu](rep.db, "SELECT waifus.* FROM waifus WHERE owner_id=$1;", userId)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
userRep := newUserRepository(rep.db)
|
||||||
|
user, err := userRep.GetById(userId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, waifu := range waifus {
|
for _, waifu := range waifus {
|
||||||
waifu.Owner = new(User)
|
waifu.Owner = user
|
||||||
err = rep.db.Get(waifu.Owner, "SELECT * FROM users WHERE id=$1;", waifu.OwnerID.Int64)
|
|
||||||
if err != nil {
|
|
||||||
return waifus, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return waifus, nil
|
return waifus, nil
|
||||||
}
|
}
|
||||||
@@ -69,8 +68,8 @@ func (rep *WaifuRepository) GetCountByUserId(userId int) (int64, error) {
|
|||||||
return count, err
|
return count, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rep *WaifuRepository) GetFree() ([]*Waifu, error) {
|
func (rep *WaifuRepository) GetFree() ([]Waifu, error) {
|
||||||
waifus, err := sqlx.List[*Waifu](rep.db, "SELECT * FROM waifus WHERE owner_id IS NULL;")
|
waifus, err := sqlx.List[Waifu](rep.db, "SELECT * FROM waifus WHERE owner_id IS NULL;")
|
||||||
return waifus, err
|
return waifus, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,16 +79,16 @@ func (rep *WaifuRepository) GetFreeCount() (int64, error) {
|
|||||||
return count, err
|
return count, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rep *WaifuRepository) GetFreeByRarity(rarity int) ([]*Waifu, error) {
|
func (rep *WaifuRepository) GetFreeByRarity(rarity int) ([]Waifu, error) {
|
||||||
waifus, err := sqlx.List[*Waifu](rep.db, "SELECT * FROM waifus WHERE owner_id IS NULL AND rarity=$1;", rarity)
|
waifus, err := sqlx.List[Waifu](rep.db, "SELECT * FROM waifus WHERE owner_id IS NULL AND rarity=$1;", rarity)
|
||||||
return waifus, err
|
return waifus, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rep *WaifuRepository) GetById(id int) (*Waifu, error) {
|
func (rep *WaifuRepository) GetById(id int) (Waifu, error) {
|
||||||
waifu := new(Waifu)
|
waifu := Waifu{}
|
||||||
err := rep.db.Get(waifu, "SELECT * FROM waifus WHERE id=$1;", id)
|
err := rep.db.Get(&waifu, "SELECT * FROM waifus WHERE id=$1;", id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return waifu, err
|
||||||
}
|
}
|
||||||
if !waifu.OwnerID.Valid {
|
if !waifu.OwnerID.Valid {
|
||||||
return waifu, err
|
return waifu, err
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
package psql
|
package psql
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"kurumibot/database"
|
|
||||||
|
|
||||||
"git.nix13.pw/scuroneko/laniakea"
|
"git.nix13.pw/scuroneko/laniakea"
|
||||||
"github.com/shopspring/decimal"
|
"github.com/shopspring/decimal"
|
||||||
"github.com/vinovest/sqlx"
|
"github.com/vinovest/sqlx"
|
||||||
@@ -21,18 +19,21 @@ type WorkRepository struct {
|
|||||||
db *sqlx.DB
|
db *sqlx.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWorkRepository(db *laniakea.DatabaseContext) *WorkRepository {
|
func newWorkRepository(db *sqlx.DB) WorkRepository {
|
||||||
return &WorkRepository{db: db.PostgresSQL}
|
return WorkRepository{db}
|
||||||
|
}
|
||||||
|
func NewWorkRepository(db *laniakea.DatabaseContext) WorkRepository {
|
||||||
|
return newWorkRepository(db.PostgresSQL)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rep *WorkRepository) GetById(id int) (Work, error) {
|
func (rep WorkRepository) GetById(id int) (Work, error) {
|
||||||
work := Work{}
|
work := Work{}
|
||||||
err := database.PostgresDatabase.Get(&work, "SELECT * FROM works WHERE id = $1;", id)
|
err := rep.db.Get(&work, "SELECT * FROM works WHERE id = $1;", id)
|
||||||
return work, err
|
return work, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rep *WorkRepository) GetAll() ([]Work, error) {
|
func (rep WorkRepository) GetAll() ([]Work, error) {
|
||||||
works := make([]Work, 0)
|
works := make([]Work, 0)
|
||||||
err := database.PostgresDatabase.Select(&works, "SELECT * FROM works;")
|
err := rep.db.Select(&works, "SELECT * FROM works;")
|
||||||
return works, err
|
return works, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,19 +12,19 @@ type AiRepository struct {
|
|||||||
client *redis.Client
|
client *redis.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAiRepository(db *laniakea.DatabaseContext) *AiRepository {
|
func NewAiRepository(db *laniakea.DatabaseContext) AiRepository {
|
||||||
return &AiRepository{client: db.Redis}
|
return AiRepository{client: db.Redis}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rep *AiRepository) SetChatId(userId int, chatId string) error {
|
func (rep AiRepository) SetChatId(userId int, chatId string) error {
|
||||||
key := fmt.Sprintf("ai.chats.gpt.%d", userId)
|
key := fmt.Sprintf("ai.chats.gpt.%d", userId)
|
||||||
return rep.client.Set(ctx, key, chatId, 0).Err()
|
return rep.client.Set(ctx, key, chatId, 0).Err()
|
||||||
}
|
}
|
||||||
func (rep *AiRepository) GetChatId(userId int) (string, error) {
|
func (rep AiRepository) GetChatId(userId int) (string, error) {
|
||||||
key := fmt.Sprintf("ai.chats.gpt.%d", userId)
|
key := fmt.Sprintf("ai.chats.gpt.%d", userId)
|
||||||
return rep.client.Get(ctx, key).Result()
|
return rep.client.Get(ctx, key).Result()
|
||||||
}
|
}
|
||||||
func (rep *AiRepository) GetOrCreateChatId(userId int) (string, error) {
|
func (rep AiRepository) GetOrCreateChatId(userId int) (string, error) {
|
||||||
key := fmt.Sprintf("ai.chats.gpt.%d", userId)
|
key := fmt.Sprintf("ai.chats.gpt.%d", userId)
|
||||||
res := rep.client.Get(ctx, key)
|
res := rep.client.Get(ctx, key)
|
||||||
if res.Err() != nil {
|
if res.Err() != nil {
|
||||||
|
|||||||
@@ -17,15 +17,15 @@ type RPRepository struct {
|
|||||||
client *redis.Client
|
client *redis.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRPRepository(db *laniakea.DatabaseContext) *RPRepository {
|
func NewRPRepository(db *laniakea.DatabaseContext) RPRepository {
|
||||||
return &RPRepository{client: db.Redis}
|
return RPRepository{client: db.Redis}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rep *RPRepository) SetSelectedWaifu(userId, waifuId int) error {
|
func (rep RPRepository) SetSelectedWaifu(userId, waifuId int) error {
|
||||||
key := fmt.Sprintf("ai.chats.rp.%d", userId)
|
key := fmt.Sprintf("ai.chats.rp.%d", userId)
|
||||||
return rep.client.Set(ctx, key, waifuId, 0).Err()
|
return rep.client.Set(ctx, key, waifuId, 0).Err()
|
||||||
}
|
}
|
||||||
func (rep *RPRepository) GetSelectedWaifu(userId int) int {
|
func (rep RPRepository) GetSelectedWaifu(userId int) int {
|
||||||
key := fmt.Sprintf("ai.chats.rp.%d", userId)
|
key := fmt.Sprintf("ai.chats.rp.%d", userId)
|
||||||
res := rep.client.Get(ctx, key)
|
res := rep.client.Get(ctx, key)
|
||||||
if res.Err() != nil {
|
if res.Err() != nil {
|
||||||
@@ -35,11 +35,11 @@ func (rep *RPRepository) GetSelectedWaifu(userId int) int {
|
|||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rep *RPRepository) SetChatId(userId, waifuId int, chatId string) error {
|
func (rep RPRepository) SetChatId(userId, waifuId int, chatId string) error {
|
||||||
key := fmt.Sprintf("ai.chats.rp.%d.%d.chat", userId, waifuId)
|
key := fmt.Sprintf("ai.chats.rp.%d.%d.chat", userId, waifuId)
|
||||||
return rep.client.Set(context.Background(), key, chatId, 0).Err()
|
return rep.client.Set(context.Background(), key, chatId, 0).Err()
|
||||||
}
|
}
|
||||||
func (rep *RPRepository) GetChatId(userId, waifuId int) string {
|
func (rep RPRepository) GetChatId(userId, waifuId int) string {
|
||||||
key := fmt.Sprintf("ai.chats.rp.%d.%d.chat", userId, waifuId)
|
key := fmt.Sprintf("ai.chats.rp.%d.%d.chat", userId, waifuId)
|
||||||
res := rep.client.Get(ctx, key)
|
res := rep.client.Get(ctx, key)
|
||||||
if res.Err() != nil {
|
if res.Err() != nil {
|
||||||
@@ -47,7 +47,7 @@ func (rep *RPRepository) GetChatId(userId, waifuId int) string {
|
|||||||
}
|
}
|
||||||
return res.Val()
|
return res.Val()
|
||||||
}
|
}
|
||||||
func (rep *RPRepository) GetOrCreateChatId(userId, waifuId int) (string, error) {
|
func (rep RPRepository) GetOrCreateChatId(userId, waifuId int) (string, error) {
|
||||||
chatId := rep.GetChatId(userId, waifuId)
|
chatId := rep.GetChatId(userId, waifuId)
|
||||||
if chatId == "" {
|
if chatId == "" {
|
||||||
chatId = uuid.New().String()
|
chatId = uuid.New().String()
|
||||||
@@ -56,11 +56,11 @@ func (rep *RPRepository) GetOrCreateChatId(userId, waifuId int) (string, error)
|
|||||||
return chatId, err
|
return chatId, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rep *RPRepository) SetChatPrompt(userId, waifuId int, prompt string) error {
|
func (rep RPRepository) SetChatPrompt(userId, waifuId int, prompt string) error {
|
||||||
key := fmt.Sprintf("ai.chats.rp.%d.%d.prompt", userId, waifuId)
|
key := fmt.Sprintf("ai.chats.rp.%d.%d.prompt", userId, waifuId)
|
||||||
return rep.client.Set(context.Background(), key, prompt, 0).Err()
|
return rep.client.Set(context.Background(), key, prompt, 0).Err()
|
||||||
}
|
}
|
||||||
func (rep *RPRepository) GetChatPrompt(userId, waifuId int) string {
|
func (rep RPRepository) GetChatPrompt(userId, waifuId int) string {
|
||||||
key := fmt.Sprintf("ai.chats.rp.%d.%d.prompt", userId, waifuId)
|
key := fmt.Sprintf("ai.chats.rp.%d.%d.prompt", userId, waifuId)
|
||||||
res := rep.client.Get(ctx, key)
|
res := rep.client.Get(ctx, key)
|
||||||
if res.Err() != nil {
|
if res.Err() != nil {
|
||||||
@@ -69,11 +69,11 @@ func (rep *RPRepository) GetChatPrompt(userId, waifuId int) string {
|
|||||||
return res.Val()
|
return res.Val()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rep *RPRepository) SetCounter(userId, waifuId, counter int) error {
|
func (rep RPRepository) SetCounter(userId, waifuId, counter int) error {
|
||||||
key := fmt.Sprintf("ai.chats.rp.%d.%d.counter", userId, waifuId)
|
key := fmt.Sprintf("ai.chats.rp.%d.%d.counter", userId, waifuId)
|
||||||
return rep.client.Set(ctx, key, counter, 0).Err()
|
return rep.client.Set(ctx, key, counter, 0).Err()
|
||||||
}
|
}
|
||||||
func (rep *RPRepository) GetCounter(userId, waifuId int) int {
|
func (rep RPRepository) GetCounter(userId, waifuId int) int {
|
||||||
key := fmt.Sprintf("ai.chats.rp.%d.%d.counter", userId, waifuId)
|
key := fmt.Sprintf("ai.chats.rp.%d.%d.counter", userId, waifuId)
|
||||||
res := rep.client.Get(ctx, key)
|
res := rep.client.Get(ctx, key)
|
||||||
if res.Err() != nil {
|
if res.Err() != nil {
|
||||||
@@ -83,11 +83,11 @@ func (rep *RPRepository) GetCounter(userId, waifuId int) int {
|
|||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rep *RPRepository) SetChatTokens(userId, waifuId, tokens int) error {
|
func (rep RPRepository) SetChatTokens(userId, waifuId, tokens int) error {
|
||||||
key := fmt.Sprintf("ai.chats.rp.%d.%d.tokens", userId, waifuId)
|
key := fmt.Sprintf("ai.chats.rp.%d.%d.tokens", userId, waifuId)
|
||||||
return rep.client.Set(ctx, key, tokens, 0).Err()
|
return rep.client.Set(ctx, key, tokens, 0).Err()
|
||||||
}
|
}
|
||||||
func (rep *RPRepository) GetChatTokens(userId, waifuId int) int {
|
func (rep RPRepository) GetChatTokens(userId, waifuId int) int {
|
||||||
key := fmt.Sprintf("ai.chats.rp.%d.%d.tokens", userId, waifuId)
|
key := fmt.Sprintf("ai.chats.rp.%d.%d.tokens", userId, waifuId)
|
||||||
res := rep.client.Get(ctx, key)
|
res := rep.client.Get(ctx, key)
|
||||||
if res.Err() != nil {
|
if res.Err() != nil {
|
||||||
@@ -97,11 +97,11 @@ func (rep *RPRepository) GetChatTokens(userId, waifuId int) int {
|
|||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rep *RPRepository) SetChatSettingID(userId, waifuId, settingId int) error {
|
func (rep RPRepository) SetChatSettingID(userId, waifuId, settingId int) error {
|
||||||
key := fmt.Sprintf("ai.chats.rp.%d.%d.setting_id", userId, waifuId)
|
key := fmt.Sprintf("ai.chats.rp.%d.%d.setting_id", userId, waifuId)
|
||||||
return rep.client.Set(ctx, key, settingId, 0).Err()
|
return rep.client.Set(ctx, key, settingId, 0).Err()
|
||||||
}
|
}
|
||||||
func (rep *RPRepository) GetChatSettingID(userId, waifuId int) int {
|
func (rep RPRepository) GetChatSettingID(userId, waifuId int) int {
|
||||||
key := fmt.Sprintf("ai.chats.rp.%d.%d.setting_id", userId, waifuId)
|
key := fmt.Sprintf("ai.chats.rp.%d.%d.setting_id", userId, waifuId)
|
||||||
res := rep.client.Get(ctx, key)
|
res := rep.client.Get(ctx, key)
|
||||||
if res.Err() != nil {
|
if res.Err() != nil {
|
||||||
@@ -111,11 +111,11 @@ func (rep *RPRepository) GetChatSettingID(userId, waifuId int) int {
|
|||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rep *RPRepository) SetChatScenariosIDs(userId, waifuId int, scenarioIds string) error {
|
func (rep RPRepository) SetChatScenariosIDs(userId, waifuId int, scenarioIds string) error {
|
||||||
key := fmt.Sprintf("ai.chats.rp.%d.%d.scenario_id", userId, waifuId)
|
key := fmt.Sprintf("ai.chats.rp.%d.%d.scenario_id", userId, waifuId)
|
||||||
return rep.client.Set(ctx, key, scenarioIds, 0).Err()
|
return rep.client.Set(ctx, key, scenarioIds, 0).Err()
|
||||||
}
|
}
|
||||||
func (rep *RPRepository) GetChatScenariosIDs(userId, waifuId int) []int {
|
func (rep RPRepository) GetChatScenariosIDs(userId, waifuId int) []int {
|
||||||
key := fmt.Sprintf("ai.chats.rp.%d.%d.scenario_id", userId, waifuId)
|
key := fmt.Sprintf("ai.chats.rp.%d.%d.scenario_id", userId, waifuId)
|
||||||
res := rep.client.Get(ctx, key)
|
res := rep.client.Get(ctx, key)
|
||||||
if res.Err() != nil {
|
if res.Err() != nil {
|
||||||
|
|||||||
@@ -100,7 +100,7 @@ func passiveIncome(b *laniakea.Bot) error {
|
|||||||
user.BtcIncome = user.BtcIncome.Add(btcIncome)
|
user.BtcIncome = user.BtcIncome.Add(btcIncome)
|
||||||
user.IncomeTime = time.Now()
|
user.IncomeTime = time.Now()
|
||||||
|
|
||||||
_, err = userRep.Update(user)
|
_, err = userRep.Update(&user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.Logger().Error(err)
|
b.Logger().Error(err)
|
||||||
continue
|
continue
|
||||||
|
|||||||
@@ -88,7 +88,7 @@ func (w *DatabaseWriter) Print(level slog.LogLevel, prefix string, traceback []*
|
|||||||
if messages[len(messages)-1] == "\n" {
|
if messages[len(messages)-1] == "\n" {
|
||||||
messages = messages[:len(messages)-1]
|
messages = messages[:len(messages)-1]
|
||||||
}
|
}
|
||||||
entry := &mdb.ConsoleLogEntry{
|
entry := mdb.ConsoleLogEntry{
|
||||||
Level: level.GetName(),
|
Level: level.GetName(),
|
||||||
Prefix: prefix,
|
Prefix: prefix,
|
||||||
Traceback: slog.FormatFullTraceback(traceback),
|
Traceback: slog.FormatFullTraceback(traceback),
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ func rpInfo(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var waifu *psql.Waifu
|
var waifu psql.Waifu
|
||||||
waifuId := rpRepRed.GetSelectedWaifu(ctx.FromID)
|
waifuId := rpRepRed.GetSelectedWaifu(ctx.FromID)
|
||||||
if waifuId == 0 {
|
if waifuId == 0 {
|
||||||
waifus, err := waifuRep.GetByUserId(ctx.FromID)
|
waifus, err := waifuRep.GetByUserId(ctx.FromID)
|
||||||
@@ -130,7 +130,7 @@ func rpInfo(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
|
|||||||
|
|
||||||
func rpWaifuList(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
|
func rpWaifuList(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
|
||||||
waifuRep := psql.NewWaifuRepository(db)
|
waifuRep := psql.NewWaifuRepository(db)
|
||||||
waifus := make(extypes.Slice[*psql.Waifu], 0)
|
waifus := make(extypes.Slice[psql.Waifu], 0)
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
userRep := psql.NewUserRepository(db)
|
userRep := psql.NewUserRepository(db)
|
||||||
@@ -148,7 +148,7 @@ func rpWaifuList(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
|
|||||||
ctx.Error(err)
|
ctx.Error(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
waifus = waifus.Filter(func(w *psql.Waifu) bool {
|
waifus = waifus.Filter(func(w psql.Waifu) bool {
|
||||||
return len(w.RpPrompt) > 0
|
return len(w.RpPrompt) > 0
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.nix13.pw/scuroneko/extypes"
|
||||||
"git.nix13.pw/scuroneko/laniakea"
|
"git.nix13.pw/scuroneko/laniakea"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -224,7 +225,7 @@ func waifuSearch(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var freeWaifus []*psql.Waifu
|
var freeWaifus extypes.Slice[psql.Waifu]
|
||||||
rarity := 3
|
rarity := 3
|
||||||
if rand == 0 {
|
if rand == 0 {
|
||||||
rarity = 5
|
rarity = 5
|
||||||
|
|||||||
Reference in New Issue
Block a user