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

KGTB Lottery Assessment #13

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
1 change: 1 addition & 0 deletions posttest/.gitignore
Original file line number Diff line number Diff line change
@@ -35,3 +35,4 @@ out/

### VS Code ###
.vscode/
.idea
12 changes: 12 additions & 0 deletions posttest/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# stage-1 build artifact
FROM amazoncorretto:21.0.2-alpine3.19 as builder
WORKDIR /app
ADD . .
RUN ["./gradlew","bootJar"]

# stage-2 running image
FROM gcr.io/distroless/java21-debian12:latest
WORKDIR /app
COPY --from=builder /app/build/libs/posttest-0.0.1-SNAPSHOT.jar posttest-0.0.1-SNAPSHOT.jar
EXPOSE 8888
ENTRYPOINT ["java", "-jar", "posttest-0.0.1-SNAPSHOT.jar"]
172 changes: 172 additions & 0 deletions posttest/KBTG_ASSESSMENT.postman_collection.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
{
"info": {
"_postman_id": "bc7f7b7b-726d-481e-92ee-6d0477e5c721",
"name": "KBTG_ASSESSMENT",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "32476344"
},
"item": [
{
"name": "admin",
"item": [
{
"name": "create lottery",
"request": {
"auth": {
"type": "basic",
"basic": [
{
"key": "password",
"value": "password",
"type": "string"
},
{
"key": "username",
"value": "admin",
"type": "string"
}
]
},
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\n \"ticket\": \"111111\",\n \"price\": 80,\n \"amount\": 1\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "http://localhost:8888/admin/lotteries",
"protocol": "http",
"host": [
"localhost"
],
"port": "8888",
"path": [
"admin",
"lotteries"
]
}
},
"response": []
}
]
},
{
"name": "lottery",
"item": [
{
"name": "get all tickets",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "http://localhost:8888/lotteries",
"protocol": "http",
"host": [
"localhost"
],
"port": "8888",
"path": [
"lotteries"
]
}
},
"response": []
}
]
},
{
"name": "user",
"item": [
{
"name": "buy tickets",
"request": {
"method": "POST",
"header": [],
"url": {
"raw": "http://localhost:8888/users/2/lotteries/222222",
"protocol": "http",
"host": [
"localhost"
],
"port": "8888",
"path": [
"users",
"2",
"lotteries",
"222222"
]
}
},
"response": []
},
{
"name": "my tickets",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "http://localhost:8888/users/1/lotteries",
"protocol": "http",
"host": [
"localhost"
],
"port": "8888",
"path": [
"users",
"1",
"lotteries"
]
}
},
"response": []
},
{
"name": "delete user ticket",
"request": {
"method": "DELETE",
"header": [],
"url": {
"raw": "http://localhost:8888/users/1/lotteries/345345",
"protocol": "http",
"host": [
"localhost"
],
"port": "8888",
"path": [
"users",
"1",
"lotteries",
"345345"
]
}
},
"response": []
}
]
},
{
"name": "New Request",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "http://localhost:8888/",
"protocol": "http",
"host": [
"localhost"
],
"port": "8888",
"path": [
""
]
}
},
"response": []
}
]
}
3 changes: 3 additions & 0 deletions posttest/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Postman Collection

[KBTG_ASSESSMENT.postman_collection.json](KBTG_ASSESSMENT.postman_collection.json)
44 changes: 29 additions & 15 deletions posttest/build.gradle
Original file line number Diff line number Diff line change
@@ -1,35 +1,49 @@
plugins {
id 'java'
id 'org.springframework.boot' version '3.2.2'
id 'io.spring.dependency-management' version '1.1.4'
id 'java'
id 'org.springframework.boot' version '3.2.2'
id 'io.spring.dependency-management' version '1.1.4'
id 'org.jetbrains.kotlin.jvm'
}

group = 'com.kbtg.bootcamp'
version = '0.0.1-SNAPSHOT'

java {
sourceCompatibility = '17'
}

configurations {
compileOnly {
extendsFrom annotationProcessor
}
compileOnly {
extendsFrom annotationProcessor
}
}

repositories {
mavenCentral()
mavenCentral()
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.postgresql:postgresql'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.liquibase:liquibase-core'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-api:2.2.0'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0'
implementation 'io.jsonwebtoken:jjwt-api:0.12.4'
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.12.4'
runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.12.4'
runtimeOnly 'org.postgresql:postgresql'
compileOnly 'org.projectlombok:lombok:1.18.30'
annotationProcessor 'org.projectlombok:lombok:1.18.30'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.testcontainers:junit-jupiter'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
// testImplementation 'com.h2database:h2:2.2.224'
}

tasks.named('test') {
useJUnitPlatform()
useJUnitPlatform()
}
kotlin {
jvmToolchain(21)
}
39 changes: 39 additions & 0 deletions posttest/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
version: '3.8'

services:
db:
image: postgres
ports:
- "25061:5432"
restart: always
environment:
POSTGRES_DB: assessment
POSTGRES_USER: root
POSTGRES_PASSWORD: 12345678
volumes:
- postgres-data:/var/lib/postgresql/data
networks:
- lotteryservice-network
lottery-service:
build:
context: .
dockerfile: Dockerfile
restart: always
ports:
- "8888:8888"
environment:
SPRING_PROFILES_ACTIVE: "local"
SPRING_DATASOURCE_URL: jdbc:postgresql://db:5432/assessment?searchpath=public&createDatabaseIfNotExist=true&useUnicode=yes&characterEncoding=UTF-8&serverTimezone=Asia/Bangkok&prepareThreshold=0
SPRING_DATASOURCE_USERNAME: root
SPRING_DATASOURCE_PASSWORD: 12345678
depends_on:
- db
networks:
- lotteryservice-network

volumes:
postgres-data:

networks:
lotteryservice-network:
driver: bridge
8 changes: 8 additions & 0 deletions posttest/settings.gradle
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
pluginManagement {
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.9.22'
}
}
plugins {
id 'org.gradle.toolchains.foojay-resolver-convention' version '0.5.0'
}
rootProject.name = 'posttest'
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.kbtg.bootcamp.posttest;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/")
public class IndexController {

@GetMapping
public String index() {
return "Hello World!";
}
}
Original file line number Diff line number Diff line change
@@ -6,8 +6,7 @@
@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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.kbtg.bootcamp.posttest.admin.controller;

import com.kbtg.bootcamp.posttest.helper.ResponseHandler;
import com.kbtg.bootcamp.posttest.lottery.request.CreateLotteryRequest;
import com.kbtg.bootcamp.posttest.lottery.response.CreateLotteryResponse;
import com.kbtg.bootcamp.posttest.lottery.service.LotteryService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

private final LotteryService lotteryService;

@Autowired
public AdminController(LotteryService lotteryService) {
this.lotteryService = lotteryService;
}

@PreAuthorize("hasRole('ROLE_ADMIN')")
@PostMapping("/lotteries")
public ResponseEntity<Object> createLottery(
@Valid @RequestBody CreateLotteryRequest createLotteryRequest
) {
try {
if (!lotteryService.checkExistTicket(createLotteryRequest.getTicket())) {
String ticket = lotteryService.createLottery(createLotteryRequest);
return ResponseHandler.generateResponse(
"create lottery Success",
HttpStatus.CREATED,
new CreateLotteryResponse(ticket));
} else {
return ResponseHandler.generateResponse(
"ticket" + createLotteryRequest.getTicket() + " already exists",
HttpStatus.CONFLICT,
null);
}
} catch (Exception e) {
return ResponseHandler.generateResponse(e.getLocalizedMessage(),
HttpStatus.INTERNAL_SERVER_ERROR,
null);
}
}
}
Loading