Skip to content

Commit

Permalink
Merge pull request #43 from datasektionen/feat/nv/duplicate
Browse files Browse the repository at this point in the history
feat: add duplicate button
  • Loading branch information
niklasvatn authored Mar 29, 2023
2 parents ba16c04 + 48fc46a commit 7d0889f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,25 @@ public String newForm(Authentication auth, Model model) {
return "edit";
}

@GetMapping("/admin/duplicate")
public String duplicateForm(@RequestParam(name = "id") Long id, Authentication auth, Model model) {
var user = (DAuthUserDetails) auth.getPrincipal();
var baseItem = itemRepository.findById(id).orElseThrow(ResourceNotFoundException::new);

if (baseItem == null)
throw new ResourceNotFoundException();

var item = baseItem.duplicate();

item.setAuthor(user.getUser());
item.setAuthorDisplay(user.getName());
model.addAttribute("item", item);
model.addAttribute("now", LocalDateTime.now().format(formatter));
model.addAttribute("formatter", formatter);

return "edit";
}

@GetMapping("/admin/edit")
public String editForm(@RequestParam(name = "id") Long id, Model model) {
var item = itemRepository.findById(id).orElseThrow(ResourceNotFoundException::new);
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/se/datasektionen/calypso/models/entities/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,21 @@ public void triggerUpdated() {
this.updated = LocalDateTime.now();
}

public Item duplicate() {
Item duplicatedItem = new Item();
duplicatedItem.titleSwedish = this.titleSwedish;
duplicatedItem.contentSwedish = this.contentSwedish;
duplicatedItem.titleEnglish = this.titleEnglish;
duplicatedItem.contentEnglish = this.contentEnglish;
duplicatedItem.itemType = this.itemType;
duplicatedItem.sensitive = this.sensitive;

if (duplicatedItem.itemType == ItemType.EVENT) {
duplicatedItem.eventLocation = this.eventLocation;
duplicatedItem.eventStartTime = this.eventStartTime.plusDays(7);
duplicatedItem.eventEndTime = this.eventEndTime.plusDays(7);
}
return duplicatedItem;
}

}
6 changes: 6 additions & 0 deletions src/main/resources/templates/edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,12 @@ <h3>Event</h3>
onclick="return window.confirm('Är du säker på att du vill ta bort posten?');"
class="button secondary-action">Radera</a>

<a th:href="@{/admin/duplicate(id=${item.id})}"
th:if="${item.id != null}"
sec:authorize="hasAuthority('post')"
class="button secondary-action">&#128203; Duplicera</a>


<span th:case="DRAFT">
<input class="button secondary-action" type="submit" name="save" value="Spara utkast" v-if="publishStatus == 'DRAFT'" />
<input class="button primary-action" type="submit" name="publish" value="Publicera" v-if="publishStatus == 'DRAFT'" />
Expand Down

0 comments on commit 7d0889f

Please sign in to comment.