|
|
package postgres
import ( "context" "database/sql" "github.com/Masterminds/squirrel" "github.com/gissleh/stufflog/internal/slerrors" "github.com/gissleh/stufflog/models" "github.com/jmoiron/sqlx" "github.com/lib/pq" "time" )
type projectRepository struct { db *sqlx.DB }
func (r *projectRepository) Find(ctx context.Context, id string) (*models.Project, error) { res := projectDBO{} err := r.db.GetContext(ctx, &res, "SELECT * FROM project WHERE project_id=$1", id) if err != nil { if err == sql.ErrNoRows { return nil, slerrors.NotFound("Project") }
return nil, err }
return res.ToProject(), nil }
func (r *projectRepository) List(ctx context.Context, filter models.ProjectFilter) ([]*models.Project, error) { sq := squirrel.Select("*").From("project").PlaceholderFormat(squirrel.Dollar) sq = sq.Where(squirrel.Eq{"user_id": filter.UserID}) if filter.IDs != nil { sq = sq.Where(squirrel.Eq{"project_id": filter.IDs}) } if filter.Active != nil { if filter.IncludeSemiActive { sq = sq.Where(squirrel.Or{ squirrel.Eq{"active": *filter.Active}, squirrel.Eq{"status_tag": []string{ "to do", "background", "on hold", "progress", }}, }) } else { sq = sq.Where(squirrel.Eq{"active": *filter.Active}) }
} if filter.Expiring { sq = sq.Where("end_time IS NOT NULL") } if filter.Favorite != nil { sq = sq.Where(squirrel.Eq{"favorite": *filter.Favorite}) } if filter.Ungrouped == true { sq = sq.Where("project_group_id is NULL") } else if filter.ProjectGroupIDs != nil { sq = sq.Where(squirrel.Eq{"project_group_id": filter.ProjectGroupIDs}) }
sq = sq.OrderBy("active", "created_time DESC")
query, args, err := sq.ToSql() if err != nil { return nil, err }
res := make([]*projectDBO, 0, 8) err = r.db.SelectContext(ctx, &res, query, args...) if err != nil { if err == sql.ErrNoRows { return []*models.Project{}, nil } return nil, err }
res2 := make([]*models.Project, len(res)) for i, v := range res { res2[i] = v.ToProject() }
return res2, nil }
func (r *projectRepository) Insert(ctx context.Context, project models.Project) error { if project.Tags == nil { project.Tags = []string{} }
_, err := r.db.NamedExecContext(ctx, ` INSERT INTO project( project_id, user_id, project_group_id, name, description, icon, active, created_time, start_time, end_time, subtract_amount, status_tag, favorite, tags ) VALUES ( :project_id, :user_id, :project_group_id, :name, :description, :icon, :active, :created_time, :start_time, :end_time, :subtract_amount, :status_tag, :favorite, :tags ) `, toProjectDBO(project)) if err != nil { return err }
return nil }
func (r *projectRepository) Update(ctx context.Context, project models.Project) error { if project.Tags == nil { project.Tags = []string{} }
_, err := r.db.NamedExecContext(ctx, ` UPDATE project SET name = :name, description = :description, project_group_id = :project_group_id, icon = :icon, active = :active, start_time = :start_time, end_time = :end_time, subtract_amount = :subtract_amount, status_tag = :status_tag, favorite = :favorite, tags = :tags WHERE project_id=:project_id `, toProjectDBO(project)) if err != nil { return err }
return nil }
func (r *projectRepository) Delete(ctx context.Context, project models.Project) error { _, err := r.db.ExecContext(ctx, `DELETE FROM project WHERE project_id=$1`, project.ID) if err != nil { if err == sql.ErrNoRows { return slerrors.NotFound("Project") } return err }
_, err = r.db.ExecContext(ctx, `DELETE FROM task_link WHERE project_id=$1`, project.ID) if err != nil && err != sql.ErrNoRows { return err }
return nil }
type projectDBO struct { ID string `json:"id" db:"project_id"` UserID string `json:"-" db:"user_id"` ProjectGroupID *string `json:"projectGroupID" db:"project_group_id"` Name string `json:"name" db:"name"` Description string `json:"description" db:"description"` Icon string `json:"icon" db:"icon"` Active bool `json:"active" db:"active"` CreatedTime time.Time `json:"createdTime" db:"created_time"` StartTime *time.Time `json:"startTime" db:"start_time"` EndTime *time.Time `json:"endTime" db:"end_time"` SubtractAmount int `json:"subtractAmount" db:"subtract_amount"` StatusTag *string `json:"statusTag" db:"status_tag"` Favorite bool `json:"favorite" db:"favorite"` Tags pq.StringArray `json:"tags" db:"tags"` }
func toProjectDBO(project models.Project) *projectDBO { return &projectDBO{ ID: project.ID, UserID: project.UserID, ProjectGroupID: project.GroupID, Name: project.Name, Description: project.Description, Icon: project.Icon, Active: project.Active, CreatedTime: project.CreatedTime, StartTime: project.StartTime, EndTime: project.EndTime, SubtractAmount: project.SubtractAmount, StatusTag: project.StatusTag, Favorite: project.Favorite, Tags: project.Tags, } }
func (d *projectDBO) ToProject() *models.Project { return &models.Project{ ID: d.ID, UserID: d.UserID, GroupID: d.ProjectGroupID, Name: d.Name, Description: d.Description, Icon: d.Icon, Active: d.Active, CreatedTime: d.CreatedTime, StartTime: d.StartTime, EndTime: d.EndTime, SubtractAmount: d.SubtractAmount, StatusTag: d.StatusTag, Favorite: d.Favorite, Tags: d.Tags, } }
|