Skip to content

Commit

Permalink
#112 promocode for a specific categories: update queries and related …
Browse files Browse the repository at this point in the history
…code path
  • Loading branch information
syjer committed Jul 13, 2016
1 parent a1f8133 commit 5986da9
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void addPromoCode(@PathVariable("eventId") int eventId, @RequestBody Prom
int discount = promoCode.getDiscountType() == DiscountType.FIXED_AMOUNT ? promoCode.getDiscountInCents() : promoCode.getDiscountAsPercent();

eventManager.addPromoCode(promoCode.getPromoCode(), eventId, promoCode.getStart().toZonedDateTime(zoneId),
promoCode.getEnd().toZonedDateTime(zoneId), discount, promoCode.getDiscountType());
promoCode.getEnd().toZonedDateTime(zoneId), discount, promoCode.getDiscountType(), promoCode.getCategories());
}

@RequestMapping(value = "/events/{eventId}/promo-code/{promoCodeName}", method = POST)
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/alfio/manager/EventManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ public boolean toggleTicketLocking(String eventName, int categoryId, int ticketI
return true;
}

public void addPromoCode(String promoCode, int eventId, ZonedDateTime start, ZonedDateTime end, int discountAmount, DiscountType discountType) {
public void addPromoCode(String promoCode, int eventId, ZonedDateTime start, ZonedDateTime end, int discountAmount, DiscountType discountType, List<Integer> categoriesId) {
Validate.isTrue(promoCode.length() >= 7, "min length is 7 chars");
if(DiscountType.PERCENTAGE == discountType) {
Validate.inclusiveBetween(0, 100, discountAmount, "percentage discount must be between 0 and 100");
Expand All @@ -613,7 +613,7 @@ public void addPromoCode(String promoCode, int eventId, ZonedDateTime start, Zon
Validate.isTrue(discountAmount >= 0, "fixed discount amount cannot be less than zero");
}

promoCodeRepository.addPromoCode(promoCode, eventId, start, end, discountAmount, discountType.toString());
promoCodeRepository.addPromoCode(promoCode, eventId, start, end, discountAmount, discountType.toString(), Json.GSON.toJson(categoriesId));
}

public void deletePromoCode(int promoCodeId) {
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/alfio/model/PromoCodeDiscount.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@
import java.math.BigDecimal;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.*;

import alfio.util.Json;
import ch.digitalfondue.npjt.ConstructorAnnotationRowMapper.Column;
Expand Down Expand Up @@ -62,7 +59,8 @@ public PromoCodeDiscount(@Column("id")int id,
this.discountAmount = discountAmount;
this.discountType = discountType;
if(categories != null) {
this.categories = new TreeSet<>(Json.GSON.<List<Integer>>fromJson(categories, new TypeToken<List<Integer>>(){}.getType()));
List<Integer> categoriesId = Json.GSON.<List<Integer>>fromJson(categories, new TypeToken<List<Integer>>(){}.getType());
this.categories = categoriesId == null ? Collections.emptySet() : new TreeSet<>(categoriesId);
} else {
this.categories = Collections.emptySet();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ public interface PromoCodeDiscountRepository {
@Query("select * from promo_code where id = :id")
PromoCodeDiscount findById(@Bind("id") int id);

@Query("insert into promo_code(promo_code, event_id_fk, valid_from, valid_to, discount_amount, discount_type) "
+ " values (:promoCode, :eventId, :start, :end, :discountAmount, :discountType)")
@Query("insert into promo_code(promo_code, event_id_fk, valid_from, valid_to, discount_amount, discount_type, categories) "
+ " values (:promoCode, :eventId, :start, :end, :discountAmount, :discountType, :categories)")
int addPromoCode(@Bind("promoCode") String promoCode,
@Bind("eventId") int eventId, @Bind("start") ZonedDateTime start,
@Bind("end") ZonedDateTime end,
@Bind("discountAmount") int discountAmount,
@Bind("discountType") String discountType);
@Bind("discountType") String discountType,
@Bind("categories") String categories);

@Query("select * from promo_code where event_id_fk = :eventId and promo_code = :promoCode")
PromoCodeDiscount findPromoCodeInEvent(@Bind("eventId") int eventId, @Bind("promoCode") String promoCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ public void testTicketWithDiscount() {

TicketCategoryWithStatistic unbounded = event.getTicketCategories().stream().filter(t -> !t.isBounded()).findFirst().orElseThrow(IllegalStateException::new);

eventManager.addPromoCode("MYPROMOCODE", event.getId(), event.getBegin(), event.getEnd(), 10, PromoCodeDiscount.DiscountType.PERCENTAGE);
eventManager.addPromoCode("MYFIXEDPROMO", event.getId(), event.getBegin(), event.getEnd(), 5, PromoCodeDiscount.DiscountType.FIXED_AMOUNT);
eventManager.addPromoCode("MYPROMOCODE", event.getId(), event.getBegin(), event.getEnd(), 10, PromoCodeDiscount.DiscountType.PERCENTAGE, null);
eventManager.addPromoCode("MYFIXEDPROMO", event.getId(), event.getBegin(), event.getEnd(), 5, PromoCodeDiscount.DiscountType.FIXED_AMOUNT, null);

TicketReservationModification tr = new TicketReservationModification();
tr.setAmount(3);
Expand Down

0 comments on commit 5986da9

Please sign in to comment.