Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Submit Assessment #38

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions posttest/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM amazoncorretto:17.0.9-alpine3.18

WORKDIR /app
ADD . .
RUN ["./gradlew","bootJar"]
ENTRYPOINT ["java", "-jar", "build/libs/posttest-0.0.1-SNAPSHOT.jar"]
12 changes: 12 additions & 0 deletions posttest/Dockerfile.multi
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# stage-1 build artifact
FROM amazoncorretto:17.0.9-alpine3.18 as builder
WORKDIR /app
ADD . .
RUN ["./gradlew","bootJar"]


# stage-2 running image
FROM gcr.io/distroless/java17-debian12:latest
WORKDIR /app
COPY --from=builder /app/build/libs/posttest-0.0.1-SNAPSHOT.jar posttest-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java", "-jar", "posttest-0.0.1-SNAPSHOT.jar"]
21 changes: 21 additions & 0 deletions posttest/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ configurations {
}
}


repositories {
mavenCentral()
}
Expand All @@ -25,11 +26,31 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.postgresql:postgresql'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation group: 'org.springdoc', name: 'springdoc-openapi-starter-webmvc-ui', version: '2.3.0'
implementation 'org.springframework.boot:spring-boot-starter-security'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
}

tasks.withType(JavaCompile) {
options.compilerArgs << "-parameters"
}

tasks.register('integrationTest',Test){


description = 'Runs integration tests'
group = 'verification'
useJUnitPlatform()
filter {
include '**/IntegrationTest.*'
}
}

tasks.named('test') {
useJUnitPlatform()
}

29 changes: 29 additions & 0 deletions posttest/docker-compose.test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
version: '3.8'

networks:
integration-test-example:


services:
it_test:
image: amazoncorretto:17.0.9-alpine3.18
volumes:
- .:/app
working_dir: /app
depends_on:
- db
networks:
- integration-test-example
command: ["./gradlew","clean","integrationTest"]

db:
image: postgres:latest
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: 1234
POSTGRES_DB: lottery
restart: on-failure
volumes:
- ./db:/docker-entrypoint-initdb.d/
networks:
- integration-test-example
19 changes: 19 additions & 0 deletions posttest/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version: '3.8'

networks:
integration-test-example:

services:
db:
image: postgres:latest
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: 1234
POSTGRES_DB: lottery
restart: on-failure
ports:
- "5432:5432"
volumes:
- ./db:/docker-entrypoint-initdb.d/
networks:
- integration-test-example
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
package com.kbtg.bootcamp.posttest;

import com.kbtg.bootcamp.posttest.service.UserService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class PosttestApplication {

public static void main(String[] args) {
SpringApplication.run(PosttestApplication.class, args);
}
public static void main(String[] args) {
SpringApplication.run(PosttestApplication.class, args);

}

@Bean
CommandLineRunner init(UserService userService) {
return args -> {
userService.mockupUser();
};
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.kbtg.bootcamp.posttest.controller;

import com.kbtg.bootcamp.posttest.exception.BadRequestException;
import com.kbtg.bootcamp.posttest.request.LotteryRequest;
import com.kbtg.bootcamp.posttest.response.TicketResponse;
import com.kbtg.bootcamp.posttest.service.LotteryService;
import jakarta.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin")
public class AdminController {

private final LotteryService lotteryService;

AdminController(LotteryService lotteryService) {
this.lotteryService = lotteryService;
}

@PostMapping("/lotteries")
@ResponseStatus(HttpStatus.CREATED)
public TicketResponse addLottery(@Valid @RequestBody LotteryRequest lotteryRequest, BindingResult bindingResult)
throws Exception {

if (bindingResult.hasErrors()) {
bindingResult.getFieldErrors().stream().forEach(field -> {
throw new BadRequestException(field.getField() + " : " + field.getDefaultMessage());
});
}
return lotteryService.addTicket(lotteryRequest);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.kbtg.bootcamp.posttest.controller;

import com.kbtg.bootcamp.posttest.response.TicketsResponse;
import com.kbtg.bootcamp.posttest.service.LotteryService;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/lotteries")
public class LotteryController {

private final LotteryService lotteryService;

LotteryController(LotteryService lotteryService) {
this.lotteryService = lotteryService;
}

@ResponseStatus(HttpStatus.OK)
@GetMapping
public TicketsResponse getAllTickets() {

return lotteryService.getAllTickets();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.kbtg.bootcamp.posttest.controller;

import com.kbtg.bootcamp.posttest.response.IdResponse;
import com.kbtg.bootcamp.posttest.response.TicketResponse;
import com.kbtg.bootcamp.posttest.response.UserTicketResponse;
import com.kbtg.bootcamp.posttest.service.LotteryService;
import com.kbtg.bootcamp.posttest.service.UserService;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UserController {

private final LotteryService lotteryService;

private final UserService userService;

UserController(LotteryService lotteryService, UserService userService) {
this.userService = userService;
this.lotteryService = lotteryService;
}

@ResponseStatus(HttpStatus.OK)
@PostMapping("/{userID}/lotteries/{ticketNumber}")
public IdResponse buyTicket(@PathVariable("userID") String userID,
@PathVariable("ticketNumber") String ticketNumber) {

return lotteryService.buyTicket(userID, ticketNumber);
}

@ResponseStatus(HttpStatus.OK)
@DeleteMapping("/{userID}/lotteries/{ticketNumber}")
public TicketResponse deleteTicket(@PathVariable("userID") String userID,
@PathVariable("ticketNumber") String ticketNumber) {
return lotteryService.sellTicketBack(userID, ticketNumber);
}

@ResponseStatus(HttpStatus.OK)
@GetMapping("/{userID}/lotteries")
public UserTicketResponse getAllMyTickets(@PathVariable("userID") String userID) {
return lotteryService.getAllUserTickets(userID);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.kbtg.bootcamp.posttest.exception;

import com.kbtg.bootcamp.posttest.response.ApiErrorResponse;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.time.ZonedDateTime;

@RestControllerAdvice
public class ApiExceptionHandler {

@ExceptionHandler({TicketNotFoundException.class})
public ResponseEntity<ApiErrorResponse> handleNotFoundException(TicketNotFoundException e) {
ApiErrorResponse errorResponse = new ApiErrorResponse(e.getMessage(), HttpStatus.NOT_FOUND,
ZonedDateTime.now());
return new ResponseEntity<>(errorResponse, HttpStatus.NOT_FOUND);
}

@ExceptionHandler({BadRequestException.class})
public ResponseEntity<ApiErrorResponse> handleNotFoundException(BadRequestException e) {
ApiErrorResponse errorResponse = new ApiErrorResponse(e.getMessage(), HttpStatus.BAD_REQUEST,
ZonedDateTime.now());
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.kbtg.bootcamp.posttest.exception;

public class BadRequestException extends RuntimeException {

public BadRequestException(String message) {
super(message);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.kbtg.bootcamp.posttest.exception;

public class TicketNotFoundException extends RuntimeException {

public TicketNotFoundException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.kbtg.bootcamp.posttest.model;

import jakarta.persistence.*;

@Entity
@Table(name = "Lottery")

public class Lottery {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long ID;

@Column(name = "TicketNumber", length = 6)
private String ticketNumber;

@Column(name = "Amount")
private int amount;

@Column(name = "Price")
private int price;

public Lottery() {

}

public Lottery(String ticketNumber, int amount, int price) {

this.ticketNumber = ticketNumber;
this.amount = amount;
this.price = price;
}

public Long getID() {
return ID;
}

public void setID(Long ID) {
this.ID = ID;
}

public String getTicketNumber() {
return ticketNumber;
}

public void setTicketNumber(String ticketNumber) {
this.ticketNumber = ticketNumber;
}

public int getAmount() {
return amount;
}

public void setAmount(int amount) {
this.amount = amount;
}

public int getPrice() {
return price;
}

public void setPrice(int price) {
this.price = price;
}
}
Loading