Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/ADPRO-C11/review into sonar
Browse files Browse the repository at this point in the history
  • Loading branch information
asteriskzie committed May 26, 2024
2 parents fc18ab4 + eb85980 commit bebcfff
Show file tree
Hide file tree
Showing 13 changed files with 628 additions and 244 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ jobs:
name: Test
runs-on: ubuntu-latest
needs: build
services:
postgres:
image: postgres:latest
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: snackscription_review
ports:
- 5432:5432
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
steps:
- name: Checkout Repository
uses: actions/checkout@v4
Expand Down
89 changes: 89 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
name: Java CI Pipeline

on:
push:
branches:
- "**"
workflow_dispatch:

jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4

- name: Set up JDK 21
uses: actions/setup-java@v4
with:
distribution: "temurin"
java-version: "21"
cache: "gradle"

- name: Cache Gradle dependencies
uses: actions/cache@v4
with:
path: ~/.gradle/caches
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
- name: Make gradlew executable
run: chmod +x ./gradlew
- name: Build with Gradle
run: |
./gradlew assemble
# (Optional) Add steps for running tests and generating reports

- name: Upload Artifact
uses: actions/upload-artifact@v4
with:
name: java-app
path: build/libs/*.jar

test:
name: Test
runs-on: ubuntu-latest
needs: build
services:
postgres:
image: postgres:latest
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: snackscription_review
ports:
- 5432:5432
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
steps:
- name: Checkout Repository
uses: actions/checkout@v4

- name: Set up JDK 21
uses: actions/setup-java@v4
with:
distribution: "temurin"
java-version: "21"
cache: "gradle"


- name: Make gradlew executable
run: chmod +x ./gradlew


- name: Cache Gradle dependencies
uses: actions/cache@v4
with:
path: ~/.gradle/caches
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
- name: Test with Gradle
run: |
./gradlew check --info --stacktrace
./gradlew test
./gradlew jacocoTestReport
env:
PRODUCTION: test
# (Optional) Add steps for generating coverage report and other post-test tasks
7 changes: 5 additions & 2 deletions .monitoring/prometheus/prometheus.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
scrape_configs:
- job_name: 'MyAppMetrics'
- job_name: 'Snackscription Metrics'
metrics_path: '/actuator/prometheus'
scrape_interval: 3s
static_configs:
- targets: ['host.docker.internal:8080']
labels:
application: 'Snackscription Review'
application: 'Snackscription Review'
- targets: ['34.124.152.90']
labels:
application: 'Snackscription Review (deployed)'
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ dependencies {
implementation("org.springframework.boot:spring-boot-starter-thymeleaf")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation("io.micrometer:micrometer-registry-prometheus")
implementation("io.micrometer:micrometer-core")
compileOnly("org.projectlombok:lombok")
developmentOnly("org.springframework.boot:spring-boot-devtools")
runtimeOnly("org.postgresql:postgresql")
runtimeOnly("io.micrometer:micrometer-registry-prometheus")
annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
annotationProcessor("org.projectlombok:lombok")
testImplementation("org.springframework.boot:spring-boot-starter-test")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package snackscription.review.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import snackscription.review.model.Review;
import snackscription.review.service.ReviewServiceImpl;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/admin")
public class ReviewAdminController {
private ReviewServiceImpl reviewService;

public ReviewAdminController(ReviewServiceImpl reviewService) {
this.reviewService = reviewService;
}

@PutMapping("/subscription-boxes/{subsbox}/users/{user}/approve")
public ResponseEntity<Review> approveReview(@PathVariable String subsbox, @PathVariable String user) {
try {
Review review = reviewService.approveReview(subsbox, user);
return new ResponseEntity<>(review, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}

@PutMapping("/subscription-boxes/{subsbox}/users/{user}/reject")
public ResponseEntity<Review> rejectReview(@PathVariable String subsbox, @PathVariable String user) {
try {
Review review = reviewService.rejectReview(subsbox, user);
return new ResponseEntity<>(review, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}

@GetMapping("/subscription-boxes/{subsbox}/reviews")
public ResponseEntity<List<Review>> getSubsboxReviews(
@PathVariable String subsbox,
@RequestParam(required = false) String state) throws Exception {
List<Review> reviews = reviewService.getSubsboxReview(subsbox, state);
return ResponseEntity.ok().body(reviews);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@

import snackscription.review.model.Review;
import snackscription.review.service.ReviewService;
import snackscription.review.service.ReviewServiceImpl;

@CrossOrigin
@RestController
@RequestMapping("/reviews")
@RequestMapping("")
public class ReviewController {
private ReviewService reviewService;

Expand All @@ -28,8 +30,8 @@ public ResponseEntity<String> reviewPage() {
return ResponseEntity.ok().body("Welcome to the review service!");
}

@PostMapping("/subscription-boxes/{subsbox}")
public ResponseEntity<Review> createSubsboxReview(@RequestBody Map<String,String> body, @PathVariable String subsbox) {
@PostMapping("/subscription-boxes/{subsbox}/users/self")
public ResponseEntity<Review> createSelfSubsboxReview(@RequestBody Map<String,String> body, @PathVariable String subsbox) {
try {
String author = body.get(BODY_AUTHOR);
int rating = Integer.parseInt(body.get(BODY_RATING));
Expand All @@ -42,7 +44,7 @@ public ResponseEntity<Review> createSubsboxReview(@RequestBody Map<String,String
}
}

@GetMapping("/subscription-boxes/{subsbox}/public")
@GetMapping("/subscription-boxes/{subsbox}")
public ResponseEntity<List<Review>> getPublicSubsboxReview(@PathVariable String subsbox) {
try {
List<Review> reviews = reviewService.getSubsboxReview(subsbox, "APPROVED");
Expand All @@ -52,74 +54,39 @@ public ResponseEntity<List<Review>> getPublicSubsboxReview(@PathVariable String
}
}

@GetMapping("/subscription-boxes/{subsbox}/users/{user}")
public ResponseEntity<Review> getSelfSubsboxReview(@RequestBody Map<String,String> body, @PathVariable String subsbox, @PathVariable String user) {
@GetMapping("/subscription-boxes/{subsbox}/users/self")
public ResponseEntity<Review> getSelfSubsboxReview(@RequestBody Map<String,String> body, @PathVariable String subsbox) {
try {
String sender = body.get(BODY_AUTHOR);
if (!authenticate(sender, user)) {
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
}
Review review = reviewService.getReview(subsbox, user);
String author = body.get("author"); // TODO: nanti pakai JWT token untuk ambil sendernya
Review review = reviewService.getReview(subsbox, author);
return new ResponseEntity<>(review, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}

@PutMapping("/subscription-boxes/{subsbox}/users/{user}")
public ResponseEntity<Review> editReview(@RequestBody Map<String,String> body, @PathVariable String subsbox, @PathVariable String user) {
@PutMapping("/subscription-boxes/{subsbox}/users/self")
public ResponseEntity<Review> editSelfReview(@RequestBody Map<String,String> body, @PathVariable String subsbox) {
try {
String sender = body.get(BODY_AUTHOR);
if (!authenticate(sender, user)) {
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
}

int rating = Integer.parseInt(body.get(BODY_RATING));
String content = body.get(BODY_CONTENT);
String author = body.get("author"); // TODO: nanti pakai JWT token untuk ambil sendernya
int rating = Integer.parseInt(body.get("rating"));
String content = body.get("content");

Review review = reviewService.editReview(rating, content, subsbox, user);
Review review = reviewService.editReview(rating, content, subsbox, author);
return new ResponseEntity<>(review, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}

private boolean authenticate(String sender, String user) {
return true;
}

@DeleteMapping("/subscription-boxes/{subsbox}/users/{user}")
public ResponseEntity<Review> deleteReview(@PathVariable String subsbox, @PathVariable String user) {
@DeleteMapping("/subscription-boxes/{subsbox}/users/self")
public ResponseEntity<Review> deleteSelfReview(@RequestBody Map<String,String> body, @PathVariable String subsbox) {
try {
reviewService.deleteReview(subsbox, user);
String author = body.get("author"); // TODO: nanti pakai JWT token untuk ambil sendernya
reviewService.deleteReview(subsbox, author);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}

@GetMapping("/subscription-boxes/{subsbox}")
public List<Review> getSubsboxReview(@PathVariable String subsbox) throws Exception {
return reviewService.getSubsboxReview(subsbox, null);
}

@PutMapping("/subscription-boxes/{subsbox}/users/{user}/approve")
public ResponseEntity<Review> approveReview(@PathVariable String subsbox, @PathVariable String user) {
try {
Review review = reviewService.approveReview(subsbox, user);
return new ResponseEntity<>(review, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}

@PutMapping("/subscription-boxes/{subsbox}/users/{user}/reject")
public ResponseEntity<Review> rejectReview(@PathVariable String subsbox, @PathVariable String user) {
try {
Review review = reviewService.rejectReview(subsbox, user);
return new ResponseEntity<>(review, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,25 @@

import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;

import io.micrometer.common.lang.Nullable;
import snackscription.review.model.Review;
import snackscription.review.model.ReviewId;
import snackscription.review.model.ReviewId;
import snackscription.review.model.ReviewState;

public interface ReviewRepository extends JpaRepository<Review, ReviewId> {
@Nullable
List<Review> findByIdSubsbox(String subsbox);

@Nullable
List<Review> findByIdAuthor(String author);

@Nullable
List<Review> findByIdSubsboxAndState(String subsbox, ReviewState state);

@Nullable
Review findByIdSubsboxAndIdAuthor(String subsbox, String author);

void deleteByIdSubsboxAndIdAuthor(String subsbox, String author);
}
Loading

0 comments on commit bebcfff

Please sign in to comment.