262 lines
6.1 KiB
Go
262 lines
6.1 KiB
Go
package model
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strconv"
|
|
|
|
"github.com/jinzhu/gorm/dialects/postgres"
|
|
|
|
"forge.cadoles.com/Cadoles/guesstimate/internal/orm"
|
|
"github.com/jinzhu/gorm"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
type ProjectRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func (r *ProjectRepository) Create(ctx context.Context, title string, ownerID int64) (*Project, error) {
|
|
project := &Project{
|
|
Title: &title,
|
|
Tasks: make([]*Task, 0),
|
|
TaskCategories: NewDefaultTaskCategories(),
|
|
Params: NewDefaultProjectParams(),
|
|
}
|
|
|
|
err := orm.WithTx(ctx, r.db, func(ctx context.Context, tx *gorm.DB) error {
|
|
if err := tx.Save(project).Error; err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
|
|
err := tx.Model(project).Association("ACL").Append(&Access{
|
|
Level: LevelOwner,
|
|
UserID: ownerID,
|
|
}).Error
|
|
|
|
if err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
|
|
err = tx.Model(project).
|
|
Preload("ACL").
|
|
Preload("ACL.User").
|
|
Preload("Tasks").
|
|
Preload("Tasks.Category").
|
|
Preload("TaskCategories").
|
|
Find(project).
|
|
Error
|
|
|
|
if err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "could not create user")
|
|
}
|
|
|
|
return project, nil
|
|
}
|
|
|
|
func (r *ProjectRepository) UpdateTitle(ctx context.Context, projectID int64, title string) (*Project, error) {
|
|
project := &Project{}
|
|
project.ID = projectID
|
|
|
|
err := r.db.Transaction(func(tx *gorm.DB) error {
|
|
if err := tx.Model(project).Update("title", title).Error; err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
|
|
err := tx.Model(project).
|
|
Preload("ACL").
|
|
Preload("ACL.User").
|
|
Preload("Tasks").
|
|
Preload("Tasks.Category").
|
|
Preload("TaskCategories").
|
|
First(project, "id = ?", projectID).
|
|
Error
|
|
if err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return nil, errors.WithStack(err)
|
|
}
|
|
|
|
return project, nil
|
|
}
|
|
|
|
func (r *ProjectRepository) Search(ctx context.Context, filter *ProjectsFilter) ([]*Project, error) {
|
|
projects := make([]*Project, 0)
|
|
|
|
projectTableName := r.db.NewScope(&Project{}).TableName()
|
|
|
|
query := r.db.Table(projectTableName).
|
|
Preload("ACL").
|
|
Preload("ACL.User").
|
|
Preload("Tasks").
|
|
Preload("Tasks.Category").
|
|
Preload("TaskCategories")
|
|
|
|
if filter != nil {
|
|
if len(filter.Ids) > 0 {
|
|
query = query.Where(fmt.Sprintf("%s.id in (?)", projectTableName), filter.Ids)
|
|
}
|
|
|
|
if len(filter.OwnerIds) > 0 {
|
|
accessTableName := r.db.NewScope(&Access{}).TableName()
|
|
|
|
query = query.
|
|
Joins(
|
|
fmt.Sprintf(
|
|
"left join %s on %s.project_id = %s.id",
|
|
accessTableName, accessTableName, projectTableName,
|
|
),
|
|
).
|
|
Where(fmt.Sprintf("%s.level = ?", accessTableName), LevelOwner).
|
|
Where(fmt.Sprintf("%s.user_id IN (?)", accessTableName), filter.OwnerIds)
|
|
}
|
|
|
|
if filter.Limit != nil {
|
|
query = query.Limit(*filter.Limit)
|
|
}
|
|
|
|
if filter.Offset != nil {
|
|
query = query.Offset(*filter.Offset)
|
|
}
|
|
}
|
|
|
|
if err := query.Find(&projects).Error; err != nil {
|
|
return nil, errors.WithStack(err)
|
|
}
|
|
|
|
return projects, nil
|
|
}
|
|
|
|
func (r *ProjectRepository) AddTask(ctx context.Context, projectID int64, changes *ProjectTaskChanges) (*Task, error) {
|
|
project := &Project{}
|
|
project.ID = projectID
|
|
task := &Task{}
|
|
|
|
if changes == nil {
|
|
return nil, errors.Errorf("changes should not be nil")
|
|
}
|
|
|
|
err := r.db.Transaction(func(tx *gorm.DB) error {
|
|
if changes.Label != nil {
|
|
task.Label = changes.Label
|
|
}
|
|
|
|
if changes.CategoryID != nil {
|
|
taskCategory := &TaskCategory{}
|
|
taskCategory.ID = *changes.CategoryID
|
|
task.Category = taskCategory
|
|
}
|
|
|
|
if changes.Estimations != nil {
|
|
if task.Estimations == nil {
|
|
task.Estimations = postgres.Hstore{}
|
|
}
|
|
|
|
if changes.Estimations.Pessimistic != nil {
|
|
pessimistic := strconv.FormatFloat(*changes.Estimations.Pessimistic, 'f', 12, 64)
|
|
task.Estimations[EstimationPessimistic] = &pessimistic
|
|
}
|
|
|
|
if changes.Estimations.Likely != nil {
|
|
likely := strconv.FormatFloat(*changes.Estimations.Likely, 'f', 12, 64)
|
|
task.Estimations[EstimationLikely] = &likely
|
|
}
|
|
|
|
if changes.Estimations.Optimistic != nil {
|
|
optimistic := strconv.FormatFloat(*changes.Estimations.Optimistic, 'f', 12, 64)
|
|
task.Estimations[EstimationOptimistic] = &optimistic
|
|
}
|
|
|
|
if changes.CategoryID != nil {
|
|
taskCategory := &TaskCategory{}
|
|
if err := tx.Find(taskCategory, "id = ?", *changes.CategoryID).Error; err != nil {
|
|
return errors.Wrap(err, "could not find task category")
|
|
}
|
|
|
|
task.Category = taskCategory
|
|
}
|
|
}
|
|
|
|
if err := tx.Save(task).Error; err != nil {
|
|
return errors.Wrap(err, "could not create task")
|
|
}
|
|
|
|
err := tx.Model(project).Association("Tasks").Append(task).Error
|
|
if err != nil {
|
|
return errors.Wrap(err, "could not add task")
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "could not add task")
|
|
}
|
|
|
|
return task, nil
|
|
}
|
|
|
|
func (r *ProjectRepository) RemoveTask(ctx context.Context, projectID int64, taskID int64) error {
|
|
project := &Project{}
|
|
project.ID = projectID
|
|
|
|
err := r.db.Transaction(func(tx *gorm.DB) error {
|
|
task := &Task{}
|
|
task.ID = taskID
|
|
|
|
err := tx.Model(project).Association("Tasks").Delete(task).Error
|
|
if err != nil {
|
|
return errors.Wrap(err, "could not remove task relationship")
|
|
}
|
|
|
|
err = tx.Delete(task, "id = ?", taskID).Error
|
|
if err != nil {
|
|
return errors.Wrap(err, "could not delete task")
|
|
}
|
|
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return errors.Wrap(err, "could not remove task")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (r *ProjectRepository) UpdateTaskEstimation(ctx context.Context, projectID, taskID int64, estimation string, value float64) (*Task, error) {
|
|
err := r.db.Transaction(func(tx *gorm.DB) error {
|
|
task := &Task{}
|
|
if err := tx.First(task, "id = ?", taskID).Error; err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
|
|
strValue := strconv.FormatFloat(value, 'f', 12, 64)
|
|
task.Estimations[estimation] = &strValue
|
|
|
|
if err := tx.Save(task).Error; err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "could not update task")
|
|
}
|
|
|
|
return nil, nil
|
|
}
|
|
|
|
func NewProjectRepository(db *gorm.DB) *ProjectRepository {
|
|
return &ProjectRepository{db}
|
|
}
|