diff --git a/src/main/java/de/rwth/swc/sqa/api/CustomerController.java b/src/main/java/de/rwth/swc/sqa/api/CustomerController.java index 22f6ba22b70029239e5c9429dbf7fa794f298f33..2f683ef537062585190c749ae928a94298e93095 100644 --- a/src/main/java/de/rwth/swc/sqa/api/CustomerController.java +++ b/src/main/java/de/rwth/swc/sqa/api/CustomerController.java @@ -1,52 +1,95 @@ + package de.rwth.swc.sqa.api; -import de.rwth.swc.sqa.model.DiscountCard; -import io.swagger.annotations.ApiParam; -import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; +//import java.net.http.HttpResponse; +import java.util.ArrayList; +import java.util.List; + +import javax.validation.Valid; + import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; + +import de.rwth.swc.sqa.DataService; +import de.rwth.swc.sqa.model.Customer; +import de.rwth.swc.sqa.model.DiscountCard; +import io.swagger.annotations.ApiParam; -import javax.validation.Valid; -import java.net.http.HttpResponse; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; @Controller @RequestMapping("/customers") public class CustomerController implements CustomersApi{ + + + + @PostMapping("") + public ResponseEntity<Customer> addCustomer(@ApiParam(value = "Customer object that should be added",required = true) @Valid @RequestBody Customer body) { + if(body.getId()!=null || body.getBirthdate().equals("") ) { + return ResponseEntity.status(400).body(null); + }else if(body.getDisabled()==null) { + body.setDisabled(false); + } + body.setId(System.currentTimeMillis()); + DataService.customerList.add(body); + return ResponseEntity.status(201).body(body); + } @PostMapping("/{customerId}/discountcards") public ResponseEntity<DiscountCard> addDiscountCardToCustomer(@ApiParam(value = "ID of customer",required = true) @PathVariable("customerId") Long customerId, @ApiParam(value = "DiscountCard object that needs to be added to the customer",required = true) @RequestBody @Valid DiscountCard body) { - this.getRequest().ifPresent((request) -> { - Iterator var1 = MediaType.parseMediaTypes(request.getHeader("Accept")).iterator(); - - while(var1.hasNext()) { - MediaType mediaType = (MediaType)var1.next(); - if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) { - String exampleString = "{ \"validFor\" : \"30d\", \"customerId\" : 6, \"id\" : 0, \"validFrom\" : \"1992-01-01\", \"type\" : 1 }"; - ApiUtil.setExampleResponse(request, "application/json", exampleString); - break; - } - } - - }); - return new ResponseEntity(HttpStatus.NOT_IMPLEMENTED); + if(body.getId()==null) { + return ResponseEntity.status(400).body(null); + } + boolean exists = false; + for(Customer customer: DataService.customerList) { + if(customer.getId().longValue()==customerId.longValue()) { + exists = true; + } + } + if(!exists) { + return ResponseEntity.status(404).body(null); + } + boolean conflict=false; + if( DataService.discountCardMap.containsKey(customerId)) { + List<DiscountCard> discountCardList = DataService.discountCardMap.get(customerId); + for(DiscountCard card:discountCardList) { + + if(card.getValidFrom().equals(body.getValidFrom())) { + conflict = true; + } + } + if(conflict) { + return ResponseEntity.status(409).body(null); + }else { + discountCardList.add(body); + } + }else { + List<DiscountCard> discountCardList = new ArrayList<DiscountCard>(); + discountCardList.add(body); + DataService.discountCardMap.put(customerId, discountCardList); + } + return ResponseEntity.status(201).body(body); } @GetMapping("/{customerId}/discountcards") public ResponseEntity<List<DiscountCard>> getCustomerDiscountCards(@ApiParam(value = "ID of customer to search for discount cards",required = true) @PathVariable("customerId") Long customerId) { - List<DiscountCard> customerDiscountCards = new ArrayList<DiscountCard>(); - - DiscountCard testCard = new DiscountCard(); - testCard.setCustomerId(new Long(55443327)); - testCard.setType(1); - - customerDiscountCards.add(testCard); - - return ResponseEntity.ok().body(customerDiscountCards); + if(customerId.longValue()==0) { + return ResponseEntity.status(400).body(null); + } + boolean exists = false; + for(Customer customer: DataService.customerList) { + if(customer.getId().longValue()==customerId.longValue() && DataService.discountCardMap.containsKey(customerId)) { + exists = true; + } + } + if(!exists) { + return ResponseEntity.status(404).body(null); + } + return ResponseEntity.ok().body( DataService.discountCardMap.get(customerId)); } } diff --git a/src/main/java/de/rwth/swc/sqa/api/TicketController.java b/src/main/java/de/rwth/swc/sqa/api/TicketController.java index ab93f85e3023e845b3cb812d1160d479c4b77376..e2e9f39eb1799e6e9873cc0b270693820ba3c802 100644 --- a/src/main/java/de/rwth/swc/sqa/api/TicketController.java +++ b/src/main/java/de/rwth/swc/sqa/api/TicketController.java @@ -1,49 +1,82 @@ package de.rwth.swc.sqa.api; -import de.rwth.swc.sqa.model.Customer; +import java.text.SimpleDateFormat; +import java.util.List; + +import javax.validation.Valid; + +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import de.rwth.swc.sqa.DataService; import de.rwth.swc.sqa.model.Ticket; import de.rwth.swc.sqa.model.TicketRequest; import de.rwth.swc.sqa.model.TicketValidationRequest; -import io.swagger.annotations.*; -import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; -import org.springframework.http.ResponseEntity; -import org.springframework.validation.annotation.Validated; -import org.springframework.web.bind.annotation.*; -import org.springframework.web.context.request.NativeWebRequest; -import org.springframework.web.multipart.MultipartFile; -import springfox.documentation.annotations.ApiIgnore; - -import javax.validation.Valid; -import javax.validation.constraints.*; -import java.util.List; -import java.util.Map; -import java.util.Optional; +import io.swagger.annotations.ApiParam; @Controller @RequestMapping("/tickets") public class TicketController implements TicketsApi{ - Customer customer; @PostMapping("") public ResponseEntity<Ticket> buyTicket(@ApiParam(value = "TicketRequest object" ,required=true ) @Valid @RequestBody TicketRequest body) { - getRequest().ifPresent(request -> { - for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) { - if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) { - String exampleString = "{ \"birthdate\" : \"1992-01-01\", \"validFor\" : \"1h\", \"zone\" : \"A\", \"student\" : true, \"discountCard\" : true, \"disabled\" : true, \"id\" : 0, \"validFrom\" : \"2020-10-29T10:38:59\" }"; - ApiUtil.setExampleResponse(request, "application/json", exampleString); - break; - } - } - }); - return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED); + try { + Ticket ticket = new Ticket(); + ticket.setId(System.currentTimeMillis()); + ticket.setBirthdate(body.getBirthdate()); + + ticket.setDisabled(body.getDisabled()==null?false:body.getDisabled()); + ticket.setDiscountCard(body.getDiscountCard()==null?false:body.getDiscountCard()); + ticket.setStudent(body.getStudent()==null?false:body.getStudent()); + ticket.setValidFor(Ticket.ValidForEnum.fromValue(body.getValidFor().getValue())); + ticket.setValidFrom(body.getValidFrom()); + ticket.setZone(Ticket.ZoneEnum.fromValue(body.getZone().getValue())); + DataService.ticketList.add(ticket); + return ResponseEntity.ok().body(ticket); + }catch (Exception e) { + e.printStackTrace(); + return ResponseEntity.status(400).body(null); + } + } @PostMapping("/validate") public ResponseEntity<Void> validateTicket(@ApiParam(value = "TicketValidationRequest object that needs to validated" ,required=true ) @Valid @RequestBody TicketValidationRequest body) { - return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED); - + List<Ticket> ticketList = DataService.ticketList; + boolean valid=false; + try { + + for(Ticket ticket:ticketList) { + if(ticket.getId().longValue()==body.getTicketId().longValue() + && ticket.getDisabled().equals(body.getDisabled()) + && ticket.getStudent().equals(body.getStudent()) + && ticket.getZone().getValue().equals(body.getZone().getValue())) { + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + long startDateTime = dateFormat.parse(ticket.getValidFrom()).getTime(); + long endDateTime = dateFormat.parse(body.getDate()).getTime(); + long diff =endDateTime - startDateTime; + Ticket.ValidForEnum type= ticket.getValidFor(); + long onehour = 1000*60*60; + if( (type.equals(Ticket.ValidForEnum._1H) && diff<onehour) + || (type.equals(Ticket.ValidForEnum._1D) && diff<onehour*24) + || (type.equals(Ticket.ValidForEnum._30D) && diff<onehour*24*30) + || (type.equals(Ticket.ValidForEnum._1Y) && diff<onehour*24*30*365) ) { + valid=true; + + } + } + } + + } catch (Exception e) { + e.printStackTrace(); + } + if(valid) { + return ResponseEntity.ok().body(null); + }else { + return ResponseEntity.status(403).body(null); + } } } diff --git a/src/test/java/de/rwth/swc/sqa/DemoIntegrationTest.java b/src/test/java/de/rwth/swc/sqa/DemoIntegrationTest.java deleted file mode 100644 index 1dbcf0c5c3fafafd38b8adfba1cdc9df92c6aa73..0000000000000000000000000000000000000000 --- a/src/test/java/de/rwth/swc/sqa/DemoIntegrationTest.java +++ /dev/null @@ -1,27 +0,0 @@ -package de.rwth.swc.sqa; - -import io.restassured.RestAssured; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.web.server.LocalServerPort; - -import static io.restassured.RestAssured.given; -import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; - -@SpringBootTest(webEnvironment = RANDOM_PORT) -public class DemoIntegrationTest { - private static final String PATH = "/customers/1/discountcards"; - - @LocalServerPort - private int port; - @BeforeEach - public void setUp() { - RestAssured.port = port; - } - - @Test - public void whenReadAll_thenStatusIsNotImplemented() { - given().get(PATH).then().statusCode(501); - } -} \ No newline at end of file