Skip to content

Commit

Permalink
Merge pull request #23 from ADPRO-C11/subbox-management
Browse files Browse the repository at this point in the history
Subbox management
  • Loading branch information
pesolosep authored May 25, 2024
2 parents 7ae684c + 863dfe0 commit c436d9d
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 20 deletions.
11 changes: 9 additions & 2 deletions .monitoring/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
version: '3.7'

services:

subsbox:
image: ${REGISTRY_USER}/${IMAGE_NAME}:${IMAGE_TAG}
container_name: ${CONTAINER_NAME}
ports:
- "80:8080"

prometheus:
image: prom/prometheus:v2.44.0
container_name: prometheus
ports:
- "9090:9090"
volumes:
- ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
- ./monitoring/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml

grafana:
image: grafana/grafana:9.5.2
Expand All @@ -16,4 +23,4 @@ services:
- "3000:3000"
restart: unless-stopped
volumes:
- ./grafana/provisioning/datasources:/etc/grafana/provisioning/datasources
- ./monitoring/grafana/provisioning/datasources:/etc/grafana/provisioning/datasources
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package id.ac.ui.cs.advprog.snackscription_subscriptionbox.controller;

import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.LogAdmin;
import id.ac.ui.cs.advprog.snackscription_subscriptionbox.utils.JWTUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -21,6 +22,7 @@
@RequestMapping("/subscription-box")
@CrossOrigin(origins = "*") // Change to specific origin if needed
public class SubscriptionBoxController {

private final JWTUtils jwtUtils;
private final SubscriptionBoxService subscriptionBoxService;

Expand Down Expand Up @@ -148,4 +150,12 @@ public CompletableFuture<ResponseEntity<Optional<List<String>>>> findDistinctNam
return subscriptionBoxService.findDistinctNames()
.thenApply(ResponseEntity::ok);
}

@GetMapping("/logs")
public CompletableFuture<ResponseEntity<List<LogAdmin>>> updateSubscriptionBox(@RequestHeader(value = "Authorization") String token) throws IllegalAccessException {
validateAdminOnly(token);
return subscriptionBoxService.getLog()
.thenApply(ResponseEntity::ok)
.exceptionally(ex -> ResponseEntity.notFound().build());
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package id.ac.ui.cs.advprog.snackscription_subscriptionbox.model;

import jakarta.persistence.*;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Data
@Table(name = "Log")
@Entity
@NoArgsConstructor
public class LogAdmin {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "log_id")
private int id;

@Column(name = "log_string")
private String logString;

@Column(name = "log_sub_box")
private String subBoxId;

@Column
private LocalDateTime date;

public LogAdmin(String logString, String subBoxId) {
this.logString = logString;
this.subBoxId = subBoxId;
this.date = LocalDateTime.now();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package id.ac.ui.cs.advprog.snackscription_subscriptionbox.repository;

import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.LogAdmin;
import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.concurrent.CompletableFuture;

@Repository
public interface LogRepository extends JpaRepository<LogAdmin, Integer> {
CompletableFuture<List<LogAdmin>> findAllByOrderByDateDesc();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;

import id.ac.ui.cs.advprog.snackscription_subscriptionbox.dto.SubscriptionBoxDTO;
import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.LogAdmin;
import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox;
import org.springframework.scheduling.annotation.Async;

Expand All @@ -29,7 +30,7 @@ public interface SubscriptionBoxService {
CompletableFuture<Optional<List<SubscriptionBoxDTO>>> findByName(String name);

CompletableFuture<Optional<List<String>>> findDistinctNames();

CompletableFuture<List<LogAdmin>> getLog();


// public SubscriptionBox addBox(SubscriptionBox subscriptionBox);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,22 @@

import id.ac.ui.cs.advprog.snackscription_subscriptionbox.dto.SubscriptionBoxDTO;
import id.ac.ui.cs.advprog.snackscription_subscriptionbox.dto.DTOMapper;
import id.ac.ui.cs.advprog.snackscription_subscriptionbox.repository.LogRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.LogAdmin;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox;
import id.ac.ui.cs.advprog.snackscription_subscriptionbox.repository.SubscriptionBoxRepository;

@Service
public class SubscriptionBoxServiceImpl implements SubscriptionBoxService {
@Autowired
private SubscriptionBoxRepository subscriptionBoxRepository;

@Autowired
private LogRepository logRepository;
@Override
@Async
public CompletableFuture<SubscriptionBox> save(SubscriptionBoxDTO subscriptionBoxDTO) {
Expand Down Expand Up @@ -62,7 +66,7 @@ public CompletableFuture<SubscriptionBox> update(SubscriptionBoxDTO subscription
if (subscriptionBoxDTO == null) {
throw new IllegalArgumentException("Subscription cannot be null");
}

CompletableFuture.runAsync(() -> logUpdateStatus(subscriptionBoxDTO.getId(), "UPDATE"));
return subscriptionBoxRepository.findById(subscriptionBoxDTO.getId())
.map(subscriptionBox -> {
DTOMapper.updateSubscriptionBox(subscriptionBox, subscriptionBoxDTO);
Expand All @@ -82,6 +86,7 @@ public CompletableFuture<Void> delete(String id) {
if (subscriptionBoxRepository.findById(id).isEmpty()) {
throw new IllegalArgumentException("Subscription Box not found");
}
CompletableFuture.runAsync(() -> logUpdateStatus(id, "DELETE"));
subscriptionBoxRepository.delete(id);
return CompletableFuture.completedFuture(null);
}
Expand Down Expand Up @@ -134,5 +139,20 @@ public CompletableFuture<Optional<List<String>>> findDistinctNames() {
}


public void logUpdateStatus(String id, String status) {
String date = LocalDateTime.now().format(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss"));
String logString = "Berhasil melakukan" +status + "terhadap Subscription Box dengan ID" + id + " pada " + date;

LogAdmin log = new LogAdmin(logString, id);

logRepository.save(log);
}
@Async
public CompletableFuture<List<LogAdmin>> getLog() {
return logRepository.findAllByOrderByDateDesc();
}



}

1 change: 1 addition & 0 deletions src/main/resources/application-dev.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.format_sql=true

9 changes: 8 additions & 1 deletion src/main/resources/application-prod.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,11 @@ spring.datasource.password=${JDBC_DATABASE_PASSWORD}
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.format_sql=true
JWT_SECRET=${JWT_SECRET}
JWT_SECRET=${JWT_SECRET}

# Actuator endpoints
management.endpoints.web.exposure.include=*
management.endpoint.prometheus.enabled=true
spring.datasource.hikari.maximum-pool-size=20
# Prometheus configuration
management.prometheus.metrics.export.enabled=true
3 changes: 2 additions & 1 deletion src/main/resources/application-test.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ spring.datasource.password=${TEST_PASSWORD}
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.format_sql=true
JWT_SECRET=${JWT_SECRET}
1 change: 0 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
spring.application.name=snackscription_subscriptionbox
spring.profiles.active=${PRODUCTION:dev}
management.endpoints.web.exposure.include=*

0 comments on commit c436d9d

Please sign in to comment.