113 lines
3.5 KiB
Go
113 lines
3.5 KiB
Go
package ds
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/oklog/ulid/v2"
|
|
"github.com/timetracker/backend/internal/domain/entities"
|
|
"github.com/timetracker/backend/internal/domain/persistence"
|
|
"github.com/timetracker/backend/internal/infrastructure/persistence/db/dbo"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type TimeEntryDatasource struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewTimeEntryDatasource(db *gorm.DB) persistence.TimeEntryDatasource {
|
|
return &TimeEntryDatasource{db: db}
|
|
}
|
|
|
|
func (r *TimeEntryDatasource) Get(ctx context.Context, id ulid.ULID) (*entities.TimeEntry, error) {
|
|
var timeEntryDBO dbo.TimeEntryDBO
|
|
if err := r.db.WithContext(ctx).First(&timeEntryDBO, "id = ?", id).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
timeEntry := &entities.TimeEntry{
|
|
EntityBase: entities.EntityBase{
|
|
ID: timeEntryDBO.ID,
|
|
CreatedAt: timeEntryDBO.CreatedAt,
|
|
UpdatedAt: timeEntryDBO.UpdatedAt,
|
|
},
|
|
UserID: timeEntryDBO.UserID,
|
|
ProjectID: timeEntryDBO.ProjectID,
|
|
ActivityID: timeEntryDBO.ActivityID,
|
|
Start: timeEntryDBO.Start,
|
|
End: timeEntryDBO.End,
|
|
Description: timeEntryDBO.Description,
|
|
Billable: timeEntryDBO.Billable,
|
|
}
|
|
|
|
return timeEntry, nil
|
|
}
|
|
|
|
func (r *TimeEntryDatasource) Create(ctx context.Context, timeEntry *entities.TimeEntry) error {
|
|
timeEntryDBO := dbo.TimeEntryDBO{
|
|
ID: timeEntry.ID,
|
|
CreatedAt: timeEntry.CreatedAt,
|
|
UpdatedAt: timeEntry.UpdatedAt,
|
|
UserID: timeEntry.UserID,
|
|
ProjectID: timeEntry.ProjectID,
|
|
ActivityID: timeEntry.ActivityID,
|
|
Start: timeEntry.Start,
|
|
End: timeEntry.End,
|
|
Description: timeEntry.Description,
|
|
Billable: timeEntry.Billable,
|
|
}
|
|
|
|
return r.db.WithContext(ctx).Create(&timeEntryDBO).Error
|
|
}
|
|
|
|
func (r *TimeEntryDatasource) Update(ctx context.Context, timeEntry *entities.TimeEntry) error {
|
|
var existingEntry dbo.TimeEntryDBO
|
|
if err := r.db.WithContext(ctx).First(&existingEntry, "id = ?", timeEntry.ID).Error; err != nil {
|
|
return entities.ErrTimeEntryNotFound
|
|
}
|
|
|
|
updateData := map[string]any{
|
|
"user_id": timeEntry.UserID,
|
|
"project_id": timeEntry.ProjectID,
|
|
"activity_id": timeEntry.ActivityID,
|
|
"start": timeEntry.Start,
|
|
"end": timeEntry.End,
|
|
"description": timeEntry.Description,
|
|
"billable": timeEntry.Billable,
|
|
"updated_at": gorm.Expr("NOW()"), // Optional: Automatisches Update-Datum
|
|
}
|
|
|
|
return r.db.WithContext(ctx).Model(&dbo.TimeEntryDBO{}).Where("id = ?", timeEntry.ID).Updates(updateData).Error
|
|
}
|
|
|
|
func (r *TimeEntryDatasource) Delete(ctx context.Context, id ulid.ULID) error {
|
|
return r.db.WithContext(ctx).Delete(&dbo.TimeEntryDBO{}, "id = ?", id).Error
|
|
}
|
|
|
|
func (r *TimeEntryDatasource) GetByRange(ctx context.Context, userID ulid.ULID, from time.Time, to time.Time) ([]*entities.TimeEntry, error) {
|
|
var timeEntryDBOs []*dbo.TimeEntryDBO
|
|
if err := r.db.WithContext(ctx).Where("user_id = ? AND start_time >= ? AND end_time <= ?", userID, from, to).Find(&timeEntryDBOs).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
timeEntries := make([]*entities.TimeEntry, len(timeEntryDBOs))
|
|
for i, timeEntryDBO := range timeEntryDBOs {
|
|
timeEntries[i] = &entities.TimeEntry{
|
|
EntityBase: entities.EntityBase{
|
|
ID: timeEntryDBO.ID,
|
|
CreatedAt: timeEntryDBO.CreatedAt,
|
|
UpdatedAt: timeEntryDBO.UpdatedAt,
|
|
},
|
|
UserID: timeEntryDBO.UserID,
|
|
ProjectID: timeEntryDBO.ProjectID,
|
|
ActivityID: timeEntryDBO.ActivityID,
|
|
Start: timeEntryDBO.Start,
|
|
End: timeEntryDBO.End,
|
|
Description: timeEntryDBO.Description,
|
|
Billable: timeEntryDBO.Billable,
|
|
}
|
|
}
|
|
|
|
return timeEntries, nil
|
|
}
|