feat(ui+backend): task categories edition
This commit is contained in:
7
internal/model/error.go
Normal file
7
internal/model/error.go
Normal file
@ -0,0 +1,7 @@
|
||||
package model
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrAssociatedTaskExist = errors.New("associated task exist")
|
||||
)
|
@ -144,10 +144,6 @@ func (r *ProjectRepository) AddTask(ctx context.Context, projectID int64, change
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
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")
|
||||
@ -279,6 +275,94 @@ func (r *ProjectRepository) UpdateParams(ctx context.Context, projectID int64, c
|
||||
return project, nil
|
||||
}
|
||||
|
||||
func (r *ProjectRepository) AddTaskCategory(ctx context.Context, projectID int64, changes ProjectTaskCategoryChanges) (*TaskCategory, error) {
|
||||
project := &Project{}
|
||||
project.ID = projectID
|
||||
taskCategory := &TaskCategory{}
|
||||
|
||||
err := r.db.Transaction(func(tx *gorm.DB) error {
|
||||
if err := updateTaskCategoryWithChanges(tx, taskCategory, changes); err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
err := tx.Model(project).Association("TaskCategories").Append(taskCategory).Error
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "could not add task category")
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not add task category")
|
||||
}
|
||||
|
||||
return taskCategory, nil
|
||||
}
|
||||
|
||||
func (r *ProjectRepository) RemoveTaskCategory(ctx context.Context, projectID int64, taskCategoryID int64) error {
|
||||
project := &Project{}
|
||||
project.ID = projectID
|
||||
|
||||
err := r.db.Transaction(func(tx *gorm.DB) error {
|
||||
taskCategory := &TaskCategory{}
|
||||
taskCategory.ID = taskCategoryID
|
||||
|
||||
var totalAssociatedTasks int
|
||||
if err := tx.Model(&Task{}).Where("category_id = ?", taskCategoryID).Count(&totalAssociatedTasks).Error; err != nil {
|
||||
return errors.Wrap(err, "could not count associated tasks")
|
||||
}
|
||||
|
||||
if totalAssociatedTasks != 0 {
|
||||
return errors.WithStack(ErrAssociatedTaskExist)
|
||||
}
|
||||
|
||||
err := tx.Model(project).Association("TaskCategories").Delete(taskCategory).Error
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "could not remove task category relationship")
|
||||
}
|
||||
|
||||
err = tx.Delete(taskCategory, "id = ? AND project_id = ?", taskCategoryID, projectID).Error
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "could not delete task category")
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "could not remove task category")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *ProjectRepository) UpdateTaskCategory(ctx context.Context, projectID, taskCategoryID int64, changes ProjectTaskCategoryChanges) (*TaskCategory, error) {
|
||||
taskCategory := &TaskCategory{}
|
||||
|
||||
err := r.db.Transaction(func(tx *gorm.DB) error {
|
||||
err := tx.Model(taskCategory).
|
||||
First(taskCategory, "id = ? AND project_id = ?", taskCategoryID, projectID).
|
||||
Error
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
if err := updateTaskCategoryWithChanges(tx, taskCategory, changes); err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
if err := tx.Save(taskCategory).Error; err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not update task category")
|
||||
}
|
||||
|
||||
return taskCategory, nil
|
||||
}
|
||||
|
||||
func updateTaskWithChanges(db *gorm.DB, task *Task, changes ProjectTaskChanges) error {
|
||||
if changes.Label != nil {
|
||||
task.Label = changes.Label
|
||||
@ -330,6 +414,18 @@ func updateTaskWithChanges(db *gorm.DB, task *Task, changes ProjectTaskChanges)
|
||||
return nil
|
||||
}
|
||||
|
||||
func updateTaskCategoryWithChanges(db *gorm.DB, taskCategory *TaskCategory, changes ProjectTaskCategoryChanges) error {
|
||||
if changes.Label != nil {
|
||||
taskCategory.Label = *changes.Label
|
||||
}
|
||||
|
||||
if changes.CostPerTimeUnit != nil {
|
||||
taskCategory.CostPerTimeUnit = *changes.CostPerTimeUnit
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewProjectRepository(db *gorm.DB) *ProjectRepository {
|
||||
return &ProjectRepository{db}
|
||||
}
|
||||
|
Reference in New Issue
Block a user