package mysqldriver import ( "context" "database/sql" "git.aiterp.net/stufflog/server/internal/generate" "git.aiterp.net/stufflog/server/internal/slerrors" "git.aiterp.net/stufflog/server/models" sq "github.com/Masterminds/squirrel" "github.com/jmoiron/sqlx" ) type logRepository struct { db *sqlx.DB } func (r *logRepository) Find(ctx context.Context, id string) (*models.Log, error) { log := models.Log{} err := r.db.GetContext(ctx, &log, "SELECT * FROM log WHERE log_id=?", id) if err != nil { if err == sql.ErrNoRows { return nil, slerrors.NotFound("Log") } return nil, err } err = r.db.SelectContext(ctx, &log.Items, "SELECT * FROM log_item WHERE log_id=?", id) if err != nil { return nil, err } err = r.db.SelectContext(ctx, &log.Tasks, "SELECT * FROM log_task WHERE log_id=?", id) if err != nil { return nil, err } return &log, nil } func (r *logRepository) List(ctx context.Context, filter models.LogFilter) ([]*models.Log, error) { q := sq.Select("log.*").From("log").GroupBy("log.log_id").OrderBy("log.date") if len(filter.IssueItemIDs) > 0 || len(filter.IssueIDs) > 0 { q = q.LeftJoin("log_item ON log_item.log_id = log.log_id") } if len(filter.IssueTaskIDs) > 0 || len(filter.IssueIDs) > 0 { q = q.LeftJoin("log_task ON log_task.log_id = log.log_id") } if len(filter.IssueIDs) > 0 { q = q.Where(sq.Or{ sq.Eq{"log_task.issue_id": filter.IssueIDs}, sq.Eq{"log_item.issue_id": filter.IssueIDs}, }) } if len(filter.IssueItemIDs) > 0 { q = q.Where(sq.Eq{"log_item.issue_item_id": filter.IssueItemIDs}) } if len(filter.IssueTaskIDs) > 0 { q = q.Where(sq.Eq{"log_task.issue_task_id": filter.IssueTaskIDs}) } if len(filter.LogIDs) > 0 { q = q.Where(sq.Eq{"log.log_id": filter.LogIDs}) } if len(filter.UserIDs) > 0 { q = q.Where(sq.Eq{"log.user_id": filter.UserIDs}) } if filter.FromDate != nil { q = q.Where(sq.GtOrEq{"log.date": *filter.FromDate}) } if filter.ToDate != nil { q = q.Where(sq.LtOrEq{"log.date": *filter.ToDate}) } query, args, err := q.ToSql() if err != nil { return nil, err } results := make([]*models.Log, 0, 16) err = r.db.SelectContext(ctx, &results, query, args...) if err != nil { if err == sql.ErrNoRows { return []*models.Log{}, nil } return nil, err } err = r.fill(ctx, results) if err != nil { return nil, err } return results, nil } func (r *logRepository) Insert(ctx context.Context, log models.Log) (*models.Log, error) { tx, err := r.db.BeginTxx(ctx, nil) if err != nil { return nil, err } log.ID = generate.LogID() _, err = tx.NamedExecContext(ctx, ` INSERT INTO log ( log_id, user_id, date, description ) VALUES ( :log_id, :user_id, :date, :description ) `, log) if err != nil { _ = tx.Rollback() return nil, err } for _, item := range log.Items { item.LogID = log.ID _, err = tx.NamedExecContext(ctx, ` INSERT INTO log_item ( log_id, issue_id, issue_item_id, amount ) VALUES ( :log_id, :issue_id, :issue_item_id, :amount ) `, item) if err != nil { _ = tx.Rollback() return nil, err } } for _, task := range log.Tasks { task.LogID = log.ID _, err = tx.NamedExecContext(ctx, ` INSERT INTO log_task ( log_id, issue_id, issue_task_id, units, duration ) VALUES ( :log_id, :issue_id, :issue_task_id, :units, :duration ) `, task) if err != nil { _ = tx.Rollback() return nil, err } } err = tx.Commit() if err != nil { _ = tx.Rollback() return nil, err } return &log, nil } func (r *logRepository) Save(ctx context.Context, log models.Log) error { tx, err := r.db.BeginTxx(ctx, nil) if err != nil { return err } _, err = tx.NamedExecContext(ctx, ` UPDATE log SET date=:date, description=:description WHERE log_id=:log_id `, log) if err != nil { _ = tx.Rollback() return err } _, err = tx.ExecContext(ctx, "DELETE FROM log_item WHERE log_id=?", log.ID) if err != nil { _ = tx.Rollback() return err } _, err = tx.ExecContext(ctx, "DELETE FROM log_task WHERE log_id=?", log.ID) if err != nil { _ = tx.Rollback() return err } for _, item := range log.Items { _, err = tx.NamedExecContext(ctx, ` INSERT INTO log_item ( log_id, issue_id, issue_item_id, amount ) VALUES ( :log_id, :issue_id, :issue_item_id, :amount ) `, item) if err != nil { _ = tx.Rollback() return err } } for _, task := range log.Tasks { _, err = tx.NamedExecContext(ctx, ` INSERT INTO log_task ( log_id, issue_id, issue_task_id, units, duration ) VALUES ( :log_id, :issue_id, :issue_task_id, :units, :duration ) `, task) if err != nil { _ = tx.Rollback() return err } } err = tx.Commit() if err != nil { _ = tx.Rollback() return err } return nil } func (r *logRepository) Delete(ctx context.Context, log models.Log) error { tx, err := r.db.BeginTxx(ctx, nil) if err != nil { return err } _, err = tx.ExecContext(ctx, "DELETE FROM log WHERE log_id=?", log.ID) if err != nil { _ = tx.Rollback() return err } _, err = tx.ExecContext(ctx, "DELETE FROM log_item WHERE log_id=?", log.ID) if err != nil { _ = tx.Rollback() return err } _, err = tx.ExecContext(ctx, "DELETE FROM log_task WHERE log_id=?", log.ID) if err != nil { _ = tx.Rollback() return err } return tx.Commit() } func (r *logRepository) fill(ctx context.Context, logs []*models.Log) error { logMap := make(map[string]int, len(logs)) ids := make([]string, len(logs)) for i, log := range logs { ids[i] = log.ID logMap[log.ID] = i } itemsQuery, itemsArgs, err := sq.Select("*").From("log_item").Where(sq.Eq{"log_id": ids}).ToSql() if err != nil { return err } tasksQuery, tasksArgs, err := sq.Select("*").From("log_task").Where(sq.Eq{"log_id": ids}).ToSql() if err != nil { return err } items := make([]models.LogItem, 0, len(logs)*3) err = r.db.SelectContext(ctx, &items, itemsQuery, itemsArgs...) if err != nil { return err } for _, item := range items { log := logs[logMap[item.LogID]] log.Items = append(log.Items, item) } tasks := make([]models.LogTask, 0, len(logs)*3) err = r.db.SelectContext(ctx, &tasks, tasksQuery, tasksArgs...) if err != nil { return err } for _, task := range tasks { log := logs[logMap[task.LogID]] log.Tasks = append(log.Tasks, task) } return nil }