diff --git a/internal/changelog/repositories.go b/internal/changelog/repositories.go index ef3e1fa..e185f4c 100644 --- a/internal/changelog/repositories.go +++ b/internal/changelog/repositories.go @@ -10,16 +10,16 @@ import ( type Repository interface { GetAll(ctx context.Context, teamID uint, offset *int, limit *int, query *string) (changelogs []entities.Changelog, totalCount int, err error) - // Get(ctx context.Context, teamID, changelogID uint) (entities.Changelog, error) - // Delete(ctx context.Context, teamID, changelogID uint) error - Create(ctx context.Context, teamID uint, name string) (entities.Changelog, error) - // Update(ctx context.Context, teamID, changelogID uint, name string) (entities.Changelog, error) + Get(ctx context.Context, teamID, changelogID uint) (entities.Changelog, error) + Delete(ctx context.Context, teamID, changelogID uint) (err error) + Create(ctx context.Context, teamID uint, name string) (changelog entities.Changelog, err error) + // Update(ctx context.Context, teamID, changelogID uint, name string) (changelog entities.Changelog, err error) GetEntriesWithAuthors(ctx context.Context, teamID, changelogID uint, offset *int, limit *int, query *string) (entries []entities.ChangelogEntry, total_count int, err error) - // GetEntryWithAuthors(ctx context.Context, teamID, changelogID, entryID uint) (entities.ChangelogEntry, error) - // DeleteEntry(ctx context.Context, teamID, changelogID, entryID uint) error - // CreateEntry(ctx context.Context, teamID, changelogID uint, title, content string, authorIDs []uint) (entities.ChangelogEntry, error) - // UpdateEntry(ctx context.Context, teamID, changelogID, entryID uint, title, content string, authorIDs []uint) (entities.ChangelogEntry, error) + // GetEntryWithAuthors(ctx context.Context, teamID, changelogID, entryID uint) (entries entities.ChangelogEntry, err error) + // DeleteEntry(ctx context.Context, teamID, changelogID, entryID uint) (err error) + // CreateEntry(ctx context.Context, teamID, changelogID uint, title, content string, authorIDs []uint) (entry entities.ChangelogEntry, err error) + // UpdateEntry(ctx context.Context, teamID, changelogID, entryID uint, title, content string, authorIDs []uint) (entry entities.ChangelogEntry, err error) } type RepositoryImpl struct { @@ -60,6 +60,40 @@ func (r *RepositoryImpl) GetAll(ctx context.Context, teamID uint, offset *int, l return changelogs, int(totalCount), nil } +func (r *RepositoryImpl) Get(ctx context.Context, teamID, changelogID uint) (entities.Changelog, error) { + var changelog entities.Changelog + + result := r.db.WithContext( + ctx, + ).Where( + "team_id = ? AND id = ?", teamID, changelogID, + ).First( + &changelog, + ) + + if result.Error != nil { + return entities.Changelog{}, result.Error + } + + return changelog, nil +} + +func (r *RepositoryImpl) Delete(ctx context.Context, teamID, changelogID uint) error { + result := r.db.WithContext( + ctx, + ).Where( + "team_id = ? AND id = ?", teamID, changelogID, + ).Delete( + &entities.Changelog{}, + ) + + if result.Error != nil { + return result.Error + } + + return nil +} + func (r *RepositoryImpl) Create(ctx context.Context, teamID uint, name string) (entities.Changelog, error) { changelog := entities.Changelog{ TeamID: teamID, diff --git a/internal/changelog/service.go b/internal/changelog/service.go index 246e4a2..7eea111 100644 --- a/internal/changelog/service.go +++ b/internal/changelog/service.go @@ -9,16 +9,16 @@ import ( type Service interface { GetAll(ctx context.Context, teamID uint, offset *int, limit *int, query *string) (changelogs []entities.Changelog, totalCount int, err error) - // Get(ctx context.Context, teamID, changelogID uint) (entities.Changelog, error) - // Delete(ctx context.Context, teamID, changelogID uint) error - Create(ctx context.Context, teamID uint, name string) (entities.Changelog, error) - // Update(ctx context.Context, teamID, changelogID uint, name string) (entities.Changelog, error) + Get(ctx context.Context, teamID, changelogID uint) (changelogs entities.Changelog, err error) + Delete(ctx context.Context, teamID, changelogID uint) (err error) + Create(ctx context.Context, teamID uint, name string) (changelogs entities.Changelog, err error) + // Update(ctx context.Context, teamID, changelogID uint, name string) (changelog entities.Changelog, err error) GetEntriesWithAuthors(ctx context.Context, teamID, changelogID uint, offset *int, limit *int, query *string) (entries []entities.ChangelogEntry, total_count int, err error) - // GetEntryWithAuthors(ctx context.Context, teamID, changelogID, entryID uint) (entities.ChangelogEntry, error) - // DeleteEntry(ctx context.Context, teamID, changelogID, entryID uint) error - // CreateEntry(ctx context.Context, teamID, changelogID uint, title, content string, authorIDs []uint) (entities.ChangelogEntry, error) - // UpdateEntry(ctx context.Context, teamID, changelogID, entryID uint, title, content string, authorIDs []uint) (entities.ChangelogEntry, error) + // GetEntryWithAuthors(ctx context.Context, teamID, changelogID, entryID uint) (entries entities.ChangelogEntry, err error) + // DeleteEntry(ctx context.Context, teamID, changelogID, entryID uint) (err error) + // CreateEntry(ctx context.Context, teamID, changelogID uint, title, content string, authorIDs []uint) (entry entities.ChangelogEntry, err error) + // UpdateEntry(ctx context.Context, teamID, changelogID, entryID uint, title, content string, authorIDs []uint) (entry entities.ChangelogEntry, err error) } type ServiceImpl struct { @@ -35,6 +35,14 @@ func (s *ServiceImpl) GetAll(ctx context.Context, teamID uint, offset *int, limi return s.repo.GetAll(ctx, teamID, offset, limit, query) } +func (s *ServiceImpl) Get(ctx context.Context, teamID, changelogID uint) (entities.Changelog, error) { + return s.repo.Get(ctx, teamID, changelogID) +} + +func (s *ServiceImpl) Delete(ctx context.Context, teamID, changelogID uint) error { + return s.repo.Delete(ctx, teamID, changelogID) +} + func (s *ServiceImpl) Create(ctx context.Context, teamID uint, name string) (entities.Changelog, error) { return s.repo.Create(ctx, teamID, name) } diff --git a/internal/rest/controllers/changelogs/changelogs.go b/internal/rest/controllers/changelogs/changelogs.go index f07331d..68b7209 100644 --- a/internal/rest/controllers/changelogs/changelogs.go +++ b/internal/rest/controllers/changelogs/changelogs.go @@ -106,6 +106,8 @@ type GetChangelogResponse struct { } func (h *Handlers) GetChangelog(c hs.AuthenticatedContext) error { + ctx := c.Request().Context() + req, err := helpers.Bind[GetChangelogRequest](c) if err != nil { c.Log.WithError(err).Debug("failed to bind GetChangelogRequest") @@ -113,9 +115,25 @@ func (h *Handlers) GetChangelog(c hs.AuthenticatedContext) error { return echo.ErrBadRequest } - // TODO: implement + changelog, err := h.ChangelogsService.Get(ctx, req.TeamID, req.ChangelogID) + if err != nil { + c.Log.WithError(err).Error("failed to get changelog") - return c.JSON(http.StatusOK, req) + return echo.ErrInternalServerError + } + + resp := h.newGetChangelogResponse(changelog) + + return c.JSON(http.StatusOK, resp) +} + +func (h *Handlers) newGetChangelogResponse(changelog entities.Changelog) GetChangelogResponse { + return GetChangelogResponse{ + ID: changelog.ID, + Name: changelog.Name, + CreatedAt: changelog.CreatedAt, + UpdatedAt: changelog.UpdatedAt, + } } type DeleteChangelogRequest struct { @@ -124,6 +142,8 @@ type DeleteChangelogRequest struct { } func (h *Handlers) DeleteChangelog(c hs.AuthenticatedContext) error { + ctx := c.Request().Context() + req, err := helpers.Bind[DeleteChangelogRequest](c) if err != nil { c.Log.WithError(err).Debug("failed to bind DeleteChangelogRequest") @@ -131,9 +151,13 @@ func (h *Handlers) DeleteChangelog(c hs.AuthenticatedContext) error { return echo.ErrBadRequest } - // TODO: implement + if err := h.ChangelogsService.Delete(ctx, req.TeamID, req.ChangelogID); err != nil { + c.Log.WithError(err).Error("failed to delete changelog") - return c.JSON(http.StatusOK, req) + return echo.ErrInternalServerError + } + + return c.NoContent(http.StatusNoContent) } type PutChangelogRequest struct {