Skip to content

Commit

Permalink
implement get changelog and delete changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
naueramant committed Oct 20, 2023
1 parent 358c010 commit 656e711
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 20 deletions.
50 changes: 42 additions & 8 deletions internal/changelog/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
24 changes: 16 additions & 8 deletions internal/changelog/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down
32 changes: 28 additions & 4 deletions internal/rest/controllers/changelogs/changelogs.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,34 @@ 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")

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 {
Expand All @@ -124,16 +142,22 @@ 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")

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 {
Expand Down

0 comments on commit 656e711

Please sign in to comment.