Loggest thine Stuff
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

91 lines
2.2 KiB

package mysql
import (
"context"
"database/sql"
"encoding/json"
"git.aiterp.net/stufflog3/stufflog3/entities"
"git.aiterp.net/stufflog3/stufflog3/models"
"git.aiterp.net/stufflog3/stufflog3/ports/mysql/mysqlcore"
)
type statsRepository struct {
db *sql.DB
q *mysqlcore.Queries
}
func (r *statsRepository) Find(ctx context.Context, scopeID, statID int) (*entities.Stat, error) {
row, err := r.q.GetStat(ctx, mysqlcore.GetStatParams{ScopeID: scopeID, ID: statID})
if err != nil {
if err == sql.ErrNoRows {
return nil, models.NotFoundError("Stat")
}
return nil, err
}
var allowedAmounts []models.StatAllowedAmount
if row.AllowedAmounts.Valid {
allowedAmounts = make([]models.StatAllowedAmount, 0, 8)
_ = json.Unmarshal(row.AllowedAmounts.RawMessage, &allowedAmounts)
if len(allowedAmounts) == 0 {
allowedAmounts = nil
}
}
return &entities.Stat{
ID: row.ID,
Name: row.Name,
Weight: row.Weight,
Description: row.Description,
AllowedAmounts: allowedAmounts,
}, nil
}
func (r *statsRepository) List(ctx context.Context, scopeID int) ([]entities.Stat, error) {
rows, err := r.q.ListStats(ctx, scopeID)
if err != nil {
if err == sql.ErrNoRows {
return nil, models.NotFoundError("Stat")
}
return nil, err
}
res := make([]entities.Stat, 0, len(rows))
for _, row := range rows {
var allowedAmounts []models.StatAllowedAmount
if row.AllowedAmounts.Valid {
allowedAmounts = make([]models.StatAllowedAmount, 0, 8)
_ = json.Unmarshal(row.AllowedAmounts.RawMessage, &allowedAmounts)
if len(allowedAmounts) == 0 {
allowedAmounts = nil
}
}
res = append(res, entities.Stat{
ID: row.ID,
Name: row.Name,
Weight: row.Weight,
Description: row.Description,
AllowedAmounts: allowedAmounts,
})
}
return res, nil
}
func (r *statsRepository) Insert(ctx context.Context, stat entities.Stat) (*entities.Stat, error) {
//TODO implement me
panic("implement me")
}
func (r *statsRepository) Update(ctx context.Context, stat entities.Stat, update models.StatUpdate) error {
//TODO implement me
panic("implement me")
}
func (r *statsRepository) Delete(ctx context.Context, stat entities.Stat) error {
//TODO implement me
panic("implement me")
}