Skip to content

Commit

Permalink
feat(ketchups): Adding updateAll feature
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <bob@vibioh.fr>
  • Loading branch information
ViBiOh committed Jun 25, 2021
1 parent c1fc337 commit 384a23f
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 1 deletion.
10 changes: 9 additions & 1 deletion cmd/ketchup/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
.bg-grey,
.bg-grey:hover {
background-color: var(--grey);
color: var(--dark);
color: var(--white);
text-decoration: none;
}

Expand Down Expand Up @@ -212,6 +212,14 @@
margin: var(--space-size);
}

.margin-left {
margin-left: var(--space-size);
}

.margin-right {
margin-right: var(--space-size);
}

.margin-half {
margin: calc(var(--space-size) / 2);
}
Expand Down
8 changes: 8 additions & 0 deletions cmd/ketchup/templates/public.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@
{{ define "header-part" }}
{{ with .Root }}
{{ if ne . "/" }}
<form method="POST" action="/app/ketchups/all" class="inline">
<input type="hidden" name="method" value="PUT">

<button type="submit" class="button bg-grey margin-right" title="Update all ketchups to pattern's latest">
Update all
</button>
</form>

<a href="#create-modal" class="button bg-primary">Create</a>
{{ end }}
{{ end }}
Expand Down
11 changes: 11 additions & 0 deletions pkg/ketchup/ketchups.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ func (a app) handleCreate(w http.ResponseWriter, r *http.Request) {
}

func (a app) handleUpdate(w http.ResponseWriter, r *http.Request) {
rawID := strings.Trim(r.URL.Path, "/")
if rawID == "all" {
if err := a.ketchupService.UpdateAll(r.Context()); err != nil {
a.rendererApp.Error(w, err)
} else {
a.rendererApp.Redirect(w, r, fmt.Sprintf("%s/", appPath), renderer.NewSuccessMessage("All ketchups are up-to-date!"))
}

return
}

id, err := strconv.ParseUint(strings.Trim(r.URL.Path, "/"), 10, 64)
if err != nil {
a.rendererApp.Error(w, httpModel.WrapInvalid(err))
Expand Down
12 changes: 12 additions & 0 deletions pkg/service/ketchup/ketchup.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type App interface {
ListOutdatedByFrequency(ctx context.Context, frequency model.KetchupFrequency) ([]model.Ketchup, error)
Create(ctx context.Context, item model.Ketchup) (model.Ketchup, error)
Update(ctx context.Context, item model.Ketchup) (model.Ketchup, error)
UpdateAll(ctx context.Context) error
Delete(ctx context.Context, item model.Ketchup) error
}

Expand Down Expand Up @@ -98,6 +99,17 @@ func (a app) Create(ctx context.Context, item model.Ketchup) (model.Ketchup, err
return output, err
}

func (a app) UpdateAll(ctx context.Context) error {
err := a.ketchupStore.DoAtomic(ctx, func(ctx context.Context) error {
return a.ketchupStore.UpdateAll(ctx)
})
if err != nil {
return fmt.Errorf("unable to update all ketchup: %s", err)
}

return nil
}

func (a app) Update(ctx context.Context, item model.Ketchup) (model.Ketchup, error) {
var output model.Ketchup

Expand Down
5 changes: 5 additions & 0 deletions pkg/service/ketchup/ketchuptest/ketchuptest.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ func (a App) Update(_ context.Context, _ model.Ketchup) (model.Ketchup, error) {
return model.NoneKetchup, nil
}

// UpdateAll mocks
func (a App) UpdateAll(_ context.Context) error {
return nil
}

// Delete mocks
func (a App) Delete(_ context.Context, _ model.Ketchup) error {
return nil
Expand Down
21 changes: 21 additions & 0 deletions pkg/store/ketchup/ketchup.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type App interface {
GetByRepository(ctx context.Context, id uint64, pattern string, forUpdate bool) (model.Ketchup, error)
Create(ctx context.Context, o model.Ketchup) (uint64, error)
Update(ctx context.Context, o model.Ketchup, oldPattern string) error
UpdateAll(ctx context.Context) error
Delete(ctx context.Context, o model.Ketchup) error
}

Expand Down Expand Up @@ -333,6 +334,26 @@ func (a app) Update(ctx context.Context, o model.Ketchup, oldPattern string) err
return a.db.Exec(ctx, updateQuery, o.Repository.ID, model.ReadUser(ctx).ID, oldPattern, o.Pattern, o.Version, strings.ToLower(o.Frequency.String()))
}

const updateAllQuery = `
UPDATE
ketchup.ketchup AS k
SET
version = rv.version
FROM
ketchup.repository AS r,
ketchup.repository_version AS rv
WHERE
k.repository_id = r.id
AND k.repository_id = rv.repository_id
AND k.pattern = rv.pattern
AND k.version <> rv.version
AND k.user_id = $1
`

func (a app) UpdateAll(ctx context.Context) error {
return a.db.Exec(ctx, updateAllQuery, model.ReadUser(ctx).ID)
}

const deleteQuery = `
DELETE FROM
ketchup.ketchup
Expand Down
5 changes: 5 additions & 0 deletions pkg/store/ketchup/ketchuptest/ketchuptest.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ func (a *App) Update(_ context.Context, _ model.Ketchup, _ string) error {
return a.updateErr
}

// UpdateAll mocks
func (a *App) UpdateAll(_ context.Context) error {
return nil
}

// Delete mocks
func (a *App) Delete(_ context.Context, _ model.Ketchup) error {
return a.deleteErr
Expand Down

0 comments on commit 384a23f

Please sign in to comment.