Skip to content

Commit

Permalink
Create ticket types (#511)
Browse files Browse the repository at this point in the history
* Added API calls + logic for adding TicketTypes and TicketOptions via the admin panel

* Added some tests

* Made API calls more like REST

* Made requested changes
  • Loading branch information
bramvankooten authored and martijnjanssen committed Dec 18, 2019
1 parent eb9840b commit ab82a77
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,22 @@ public ResponseEntity<?> deleteTicketType(@PathVariable Long typeId) {
return createResponseEntity(HttpStatus.OK, "TicketType successfully deleted.");
}

@PreAuthorize("hasRole('ADMIN')")
@PutMapping("/tickettype/{typeId}")
public ResponseEntity<?> updateTicketTypeBuyable(@PathVariable Long typeId, @RequestBody @Validated TicketType ticketType) {
ticketService.updateTicketType(typeId, ticketType);
return createResponseEntity(HttpStatus.OK, "TicketType successfully updated", ticketType);
}

@PreAuthorize("hasRole('ADMIN')")
@PostMapping("/types/{typeId}/option")
public ResponseEntity<?> addOptionToType(@PathVariable Long typeId, @RequestBody @Validated TicketOption option) {
TicketType type = ticketService.getTicketTypeById(typeId);
type.addPossibleOption(option);
ticketService.updateTicketType(typeId, type);
return createResponseEntity(HttpStatus.OK, "TicketOption successfully added to Ticket Type", type);
}

@PreAuthorize("hasRole('ADMIN')")
@PostMapping("/options")
public ResponseEntity<?> addTicketOption(@RequestBody @Validated TicketOption option) {
Expand All @@ -167,6 +183,19 @@ public ResponseEntity<?> addTicketOption(@RequestBody @Validated TicketOption op
return createResponseEntity(HttpStatus.CREATED, "TicketOption successfully added", ticketOption);
}

@PreAuthorize("hasRole('ADMIN')")
@GetMapping("/options")
public Collection<TicketOption> readTicketOptions() {
return ticketService.getAllTicketOptions();
}

@PreAuthorize("hasRole('ADMIN')")
@DeleteMapping("/options/{optionId}")
public ResponseEntity<?> deleteTicketOption(@PathVariable Long optionId) {
ticketService.deleteTicketOption(optionId);
return createResponseEntity(HttpStatus.OK, "TicketOption successfully deleted.");
}

@ExceptionHandler(TicketTransferTokenException.class)
public ResponseEntity<?> handleDuplicateTicketTransFerException(TicketTransferTokenException ex) {
return createResponseEntity(HttpStatus.BAD_REQUEST, ex.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import java.util.Collection;
import java.util.List;
import java.util.Optional;

public interface TicketService {
Ticket getTicketById(Long ticketId);
Expand All @@ -45,6 +46,8 @@ public interface TicketService {
*/
void validateTicket(Long ticketId);

TicketOption getTicketOptionByName(String name);

/**
* Check if a ticket is available, and return when it is. When a ticket is unavailable (sold out for instance) a
* TicketUnavailableException is thrown
Expand Down Expand Up @@ -106,7 +109,13 @@ default Ticket requestTicketOfType(String type, List<String> options) {

TicketType updateTicketType(Long typeId, TicketType type);

TicketType getTicketTypeById(Long typeId);

void deleteTicketType(Long typeId);

TicketOption addTicketOption(TicketOption option);

void deleteTicketOption(Long optionID);

Collection<TicketOption> getAllTicketOptions();
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
Expand Down Expand Up @@ -115,7 +116,8 @@ public void validateTicket(Long ticketId) {
ticketRepository.save(ticket);
}

private TicketOption getTicketOptionByName(String name) {
@Override
public TicketOption getTicketOptionByName(String name) {
return ticketOptionRepository.findByName(name).
orElseThrow(TicketOptionNotFoundException::new);
}
Expand Down Expand Up @@ -275,6 +277,12 @@ public TicketType updateTicketType(Long typeId, TicketType type) {
return ticketTypeRepository.save(type);
}

@Override
public TicketType getTicketTypeById(Long typeId) {
return ticketTypeRepository.findById(typeId).
orElseThrow(() -> new TicketTypeNotFoundException("with Id" + typeId));
}

@Override
public void deleteTicketType(Long typeId) {
ticketTypeRepository.deleteById(typeId);
Expand All @@ -285,6 +293,15 @@ public TicketOption addTicketOption(TicketOption option) {
return ticketOptionRepository.save(option);
}

@Override
public void deleteTicketOption(Long optionId) {
ticketOptionRepository.deleteById(optionId);
}

@Override
public Collection<TicketOption> getAllTicketOptions() {
return ticketOptionRepository.findAll();
}

private TicketTransferToken getTicketTransferTokenIfValid(String token) {
TicketTransferToken ttt = tttRepository.findByToken(token).orElseThrow(() -> new TokenNotFoundException(token));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -760,4 +760,42 @@ public void testGetExportAsAdmin() {
//@formatter:on

}

@Test
public void testChangeBuyable() {
User admin = createAdmin();
TicketType ticket = getTicketType();
ticket.setBuyable(false);

//@formatter:off
given().
header(getXAuthTokenHeaderForUser(admin)).
when().
body(ticket).
contentType(ContentType.JSON).
put(TICKETS_ENDPOINT + "/tickettype/" + ticket.getId()).
then().
statusCode(HttpStatus.SC_OK).
body("object.buyable", equalTo(false));
//@formatter:on
}

@Test
public void testAddOptionToType() {
User admin = createAdmin();
TicketType ticket = getTicketType();
TicketOption option = new TicketOption("testOption", 2);
ticketService.addTicketOption(option);
//@formatter:off
given().
header(getXAuthTokenHeaderForUser(admin)).
when().
body(option).
contentType(ContentType.JSON).
post(TICKETS_ENDPOINT + "/types/" + ticket.getId() + "/option").
then().
statusCode(HttpStatus.SC_OK).
body("object.possibleOptions.id", hasItem(Math.toIntExact(option.getId())));
//@formatter:on
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import ch.wisv.areafiftylan.exception.TicketOptionNotFoundException;
import ch.wisv.areafiftylan.products.model.Ticket;
import ch.wisv.areafiftylan.products.model.TicketOption;
import ch.wisv.areafiftylan.products.model.TicketType;
import ch.wisv.areafiftylan.products.service.repository.TicketOptionRepository;
import ch.wisv.areafiftylan.products.service.repository.TicketRepository;
import ch.wisv.areafiftylan.products.service.repository.TicketTypeRepository;
Expand Down Expand Up @@ -133,6 +134,10 @@ private TicketOption getTicketOption(String option) {
return ticketOptionRepository.findByName(option).orElseThrow(TicketOptionNotFoundException::new);
}

protected TicketType getTicketType() {
return ticketTypeRepository.findByName(TEST_TICKET).orElseThrow(IllegalArgumentException::new);
}

protected Team createTeamWithCaptain(User captain) {
Team team = new Team("Team " + captain.getId(), captain);
return teamRepository.save(team);
Expand Down

0 comments on commit ab82a77

Please sign in to comment.