update borrow module + fix invite

This commit is contained in:
2021-10-27 17:07:23 +02:00
parent 3c98987678
commit 0646a21d97
19 changed files with 363 additions and 299 deletions
@@ -63,8 +63,8 @@ public class InviteManager implements UserDataProvider {
/**
* Gets the.
*
* @param page the page
* @param size the size
* @param page the page
* @param size the size
* @param search the search
* @return the page
*/
@@ -89,17 +89,17 @@ public class InviteManager implements UserDataProvider {
/**
* Gets the by owner.
*
* @param owner the owner
* @param item the item
* @param page the page
* @param size the size
* @param sortBy the sort by
* @param owner the owner
* @param item the item
* @param page the page
* @param size the size
* @param sortBy the sort by
* @param descending the descending
* @param search the search
* @param redeemed the redeemed
* @param search the search
* @param redeemed the redeemed
* @return the by owner
*/
public Page<Invite> getByOwner(Long owner, Integer item, int page, int size, String sortBy,
public Page<Invite> getByOwner(Long owner, String quota, int page, int size, String sortBy,
boolean descending, String search, String redeemed) {
PageRequest pageRequest = PageRequest.of(page, size,
descending ? Sort.by(sortBy).descending() : Sort.by(sortBy).ascending());
@@ -107,8 +107,8 @@ public class InviteManager implements UserDataProvider {
BooleanBuilder query = new BooleanBuilder();
query.and(qInvite.owner.eq(owner));
if (item != null) {
query.and(qInvite.item.eq(item));
if (StringUtils.hasText(quota)) {
query.and(qInvite.quota.eq(quota));
}
if (StringUtils.hasText(search)) {
@@ -129,20 +129,20 @@ public class InviteManager implements UserDataProvider {
/**
* Gets the others.
*
* @param owner the owner
* @param item the item
* @param page the page
* @param size the size
* @param search the search
* @param owner the owner
* @param item the item
* @param page the page
* @param size the size
* @param search the search
* @param redeemed the redeemed
* @return the others
*/
public Page<Invite> getOthers(Long owner, int item, int page, int size, String search,
public Page<Invite> getOthers(Long owner, String quota, int page, int size, String search,
String redeemed) {
BooleanBuilder query = new BooleanBuilder();
query.and(qInvite.owner.ne(owner));
query.and(qInvite.item.eq(item));
query.and(qInvite.quota.eq(quota));
if (StringUtils.hasText(search)) {
query.and(qInvite.note.containsIgnoreCase(search));
@@ -173,7 +173,8 @@ public class InviteManager implements UserDataProvider {
}
}
InviteMapping inviteMapping = inviteMappingManager.getByItem(invite.getItem());
InviteMapping inviteMapping = inviteMappingManager.getByItemAndQuota(invite.getItem(),
invite.getQuota());
Assert.notNull(inviteMapping, "No mapping for item!");
if (StringUtils.hasLength(inviteMapping.getCodeLink())) {
invite.setCodeLink(String.format(inviteMapping.getCodeLink(), invite.getCode()));
@@ -181,6 +182,8 @@ public class InviteManager implements UserDataProvider {
invite.setCodeLink(null);
}
invite.setUrl(inviteMapping.getUrl());
return inviteRepository.save(invite);
}
@@ -49,16 +49,18 @@ public class InviteMappingManager {
* @param item the item
* @return the by item
*/
public InviteMapping getByItem(int item) {
return inviteMappingRepository.findOne(qInviteMapping.item.eq(item)).orElse(null);
public InviteMapping getByItemAndQuota(int item, String quota) {
return inviteMappingRepository
.findOne(qInviteMapping.item.eq(item).and(qInviteMapping.quota.eq(quota)))
.orElse(null);
}
/**
* Gets the.
*
* @param page the page
* @param size the size
* @param sortBy the sort by
* @param page the page
* @param size the size
* @param sortBy the sort by
* @param descending the descending
* @return the page
*/
@@ -70,9 +72,9 @@ public class InviteMappingManager {
/**
* Creates the.
*
* @param quota the quota
* @param item the item
* @param starts the starts
* @param quota the quota
* @param item the item
* @param starts the starts
* @param expires the expires
* @return the invite mapping
*/
@@ -106,6 +108,16 @@ public class InviteMappingManager {
invite.setCodeLink(null);
inviteRepository.save(invite);
}
if (StringUtils.hasText(inviteMapping.getUrl())) {
if (!inviteMapping.getUrl().equals(invite.getUrl())) {
invite.setUrl(inviteMapping.getUrl());
inviteRepository.save(invite);
}
} else if (StringUtils.hasText(invite.getUrl())) {
invite.setUrl(null);
inviteRepository.save(invite);
}
}
return inviteMappingRepository.save(inviteMapping);
@@ -4,6 +4,7 @@
package de.bstly.we.invite.controller;
import java.time.Instant;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
@@ -93,24 +94,43 @@ public class InviteController extends BaseController {
invite.setNote(null);
invite.setOwner(null);
} else if (!getCurrentUserId().equals(invite.getOwner())) {
if (permissionManager.hasPermission(getCurrentUserId(), Permissions.ROLE_MEMBER)) {
invite.setId(null);
invite.setItem(null);
if (!StringUtils.hasText(invite.getNote())) {
invite.setNote("...");
}
invite.setOwner(null);
} else {
invite.setId(null);
invite.setItem(null);
invite.setId(null);
invite.setCode(null);
invite.setCodeLink(null);
invite.setOwner(null);
if (!permissionManager.hasPermission(getCurrentUserId(), Permissions.ROLE_MEMBER)) {
invite.setNote(null);
invite.setOwner(null);
}
}
return invite;
}
@GetMapping("/{code}/permissions")
public List<Permission> getPermissions(@PathVariable("code") String code) {
Invite invite = inviteManager.getByCode(code);
if (invite == null) {
throw new EntityResponseStatusException(HttpStatus.NOT_ACCEPTABLE);
}
return permissionManager.getForItem(null, invite.getItem(), new JsonArray(),
invite.getStarts(), invite.getExpires());
}
@GetMapping("/{code}/quotas")
public List<Quota> getQuotas(@PathVariable("code") String code) {
Invite invite = inviteManager.getByCode(code);
if (invite == null) {
throw new EntityResponseStatusException(HttpStatus.NOT_ACCEPTABLE);
}
List<Quota> quotas = Lists.newArrayList();
quotaManager.addForItem(null, invite.getItem(), quotas);
return quotas;
}
/**
* Register.
*
@@ -119,6 +139,11 @@ public class InviteController extends BaseController {
@PostMapping
public void register(@RequestBody UserModel userModel) {
Errors errors = new RequestBodyErrors(userModel);
if (!StringUtils.hasText(userModel.getToken())) {
throw new EntityResponseStatusException(HttpStatus.UNAUTHORIZED);
}
Invite invite = inviteManager.getByCode(userModel.getToken());
if (invite == null) {
@@ -133,7 +158,7 @@ public class InviteController extends BaseController {
boolean register = false;
for (Permission permission : permissionManager.getForItem(null, invite.getItem(),
new JsonArray(), invite.getStarts())) {
new JsonArray(), invite.getStarts(), invite.getExpires())) {
if (permission.getExpires().isAfter(Instant.now()) && !permission.isAddon()) {
register = true;
break;
@@ -180,7 +205,7 @@ public class InviteController extends BaseController {
}
permissionManager.applyItem(user.getId(), invite.getItem(), new JsonArray(),
invite.getStarts());
invite.getStarts(), invite.getExpires());
quotaManager.applyItem(user.getId(), invite.getItem());
invite.setRedeemed(true);
@@ -190,12 +215,12 @@ public class InviteController extends BaseController {
/**
* Gets the invites.
*
* @param quotaParameter the quota parameter
* @param pageParameter the page parameter
* @param sizeParameter the size parameter
* @param sortParamater the sort paramater
* @param descParameter the desc parameter
* @param searchParameter the search parameter
* @param quotaParameter the quota parameter
* @param pageParameter the page parameter
* @param sizeParameter the size parameter
* @param sortParamater the sort paramater
* @param descParameter the desc parameter
* @param searchParameter the search parameter
* @param redeemedParameter the redeemed parameter
* @return the invites
*/
@@ -208,27 +233,20 @@ public class InviteController extends BaseController {
@RequestParam("desc") Optional<Boolean> descParameter,
@RequestParam("search") Optional<String> searchParameter,
@RequestParam("redeemed") Optional<String> redeemedParameter) {
Integer item = null;
if (quotaParameter.isPresent() && StringUtils.hasText(quotaParameter.get())) {
InviteMapping inviteMapping = inviteMappingManager.get(quotaParameter.get());
if (inviteMapping == null) {
throw new EntityResponseStatusException(HttpStatus.CONFLICT);
}
item = inviteMapping.getItem();
}
return inviteManager.getByOwner(getCurrentUserId(), item, pageParameter.orElse(0),
sizeParameter.orElse(10), sortParamater.orElse("id"), descParameter.orElse(false),
searchParameter.orElse(null), redeemedParameter.orElse(null));
return inviteManager.getByOwner(getCurrentUserId(), quotaParameter.orElse(""),
pageParameter.orElse(0), sizeParameter.orElse(10), sortParamater.orElse("id"),
descParameter.orElse(false), searchParameter.orElse(null),
redeemedParameter.orElse(null));
}
/**
* Gets the other invites.
*
* @param quota the quota
* @param pageParameter the page parameter
* @param sizeParameter the size parameter
* @param searchParameter the search parameter
* @param quota the quota
* @param pageParameter the page parameter
* @param sizeParameter the size parameter
* @param searchParameter the search parameter
* @param redeemedParameter the redeemed parameter
* @return the other invites
*/
@@ -247,7 +265,7 @@ public class InviteController extends BaseController {
throw new EntityResponseStatusException(HttpStatus.CONFLICT);
}
Page<Invite> page = inviteManager.getOthers(getCurrentUserId(), inviteMapping.getItem(),
Page<Invite> page = inviteManager.getOthers(getCurrentUserId(), quota,
pageParameter.orElse(0), sizeParameter.orElse(10), searchParameter.orElse(null),
redeemedParameter.orElse(null));
for (Invite invite : page.getContent()) {
@@ -264,7 +282,7 @@ public class InviteController extends BaseController {
/**
* Creates the invite.
*
* @param quota the quota
* @param quota the quota
* @param inviteModel the invite model
* @return the invite
*/
@@ -290,6 +308,7 @@ public class InviteController extends BaseController {
invite.setExpires(inviteMapping.getExpires() != null ? inviteMapping.getExpires()
: inviteModel.getExpires());
invite.setItem(inviteMapping.getItem());
invite.setQuota(inviteMapping.getQuota());
if (inviteMapping.getMessageLimit() != null && inviteMapping.getMessageLimit() > 0
&& StringUtils.hasText(inviteModel.getMessage())
@@ -44,8 +44,8 @@ public class InviteManagingController extends BaseController {
/**
* Gets the invites.
*
* @param page the page
* @param size the size
* @param page the page
* @param size the size
* @param search the search
* @return the invites
*/
@@ -66,7 +66,8 @@ public class InviteManagingController extends BaseController {
@PreAuthorize("hasRole('ROLE_ADMIN')")
@PostMapping
public Invite createOrUpdate(@RequestBody Invite invite) {
if (invite.getItem() == null || inviteMappingManager.getByItem(invite.getItem()) == null) {
if (invite.getItem() == null || inviteMappingManager.getByItemAndQuota(invite.getItem(),
invite.getQuota()) == null) {
throw new EntityResponseStatusException(HttpStatus.CONFLICT);
}
@@ -29,6 +29,7 @@ public class Invite implements UserData {
private Long owner;
private String code;
private Integer item;
private String quota;
private Instant starts;
private Instant expires;
@Lob
@@ -36,6 +37,7 @@ public class Invite implements UserData {
private String note;
private boolean redeemed;
private String codeLink;
private String url;
/**
* Gets the id.
@@ -109,6 +111,20 @@ public class Invite implements UserData {
this.item = item;
}
/**
* @return the quota
*/
public String getQuota() {
return quota;
}
/**
* @param quota the quota to set
*/
public void setQuota(String quota) {
this.quota = quota;
}
/**
* Gets the starts.
*
@@ -216,4 +232,19 @@ public class Invite implements UserData {
public void setCodeLink(String codeLink) {
this.codeLink = codeLink;
}
/**
* @return the url
*/
public String getUrl() {
return url;
}
/**
* @param url the url to set
*/
public void setUrl(String url) {
this.url = url;
}
}
@@ -27,6 +27,7 @@ public class InviteMapping {
@Column(name = "message_limit")
private Integer messageLimit;
private String codeLink;
private String url;
private String defaultMessage;
/**
@@ -137,6 +138,20 @@ public class InviteMapping {
this.codeLink = codeLink;
}
/**
* @return the url
*/
public String getUrl() {
return url;
}
/**
* @param url the url to set
*/
public void setUrl(String url) {
this.url = url;
}
/**
* Gets the default message.
*