Skip to content

Commit

Permalink
Merge pull request #8 from ADPRO-C11/staging
Browse files Browse the repository at this point in the history
Staging
  • Loading branch information
sdikyarts authored May 15, 2024
2 parents a596c51 + e9d92a2 commit d231fc0
Show file tree
Hide file tree
Showing 21 changed files with 525 additions and 96 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ jobs:
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
- name: Replace placeholders in application.properties
run: |
sed -i "s|JDBC_DATABASE_URL=.*|JDBC_DATABASE_URL=${{ secrets.JDBC_DATABASE_URL }}|g" src/main/resources/application.properties
sed -i "s|JDBC_DATABASE_USERNAME=.*|JDBC_DATABASE_USERNAME=${{ secrets.JDBC_DATABASE_USERNAME }}|g" src/main/resources/application.properties
sed -i "s|JDBC_DATABASE_PASSWORD=.*|JDBC_DATABASE_PASSWORD=${{ secrets.JDBC_DATABASE_PASSWORD }}|g" src/main/resources/application.properties
sed -i "s|PRODUCTION=.*|PRODUCTION=${{ secrets.PRODUCTION }}|g" src/main/resources/application.properties
- name: Make gradlew executable
run: chmod +x ./gradlew
- name: Build with Gradle
Expand All @@ -43,6 +49,16 @@ jobs:
name: Test
runs-on: ubuntu-latest
needs: build
services:
postgres:
image: postgres:latest
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: super
POSTGRES_DB: subscription-admin
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 All @@ -65,6 +81,8 @@ jobs:
- name: Test with Gradle
run: |
sed -i "s|\${JDBC_DATABASE_URL}|${{ secrets.JDBC_DATABASE_URL }}|g" src/main/resources/application-prod.properties
./gradlew check --info --stacktrace
./gradlew test
./gradlew jacocoTestReport
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package snackscription.subscriptionadmin.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

@Configuration
@EnableAsync
public class AsyncConfiguration {
@Bean("asyncTaskExecutor")
public Executor asyncTaskExecutor(){
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(4);
taskExecutor.setQueueCapacity(150);
taskExecutor.setThreadNamePrefix("AsyncTaskThread-");
taskExecutor.initialize();
return taskExecutor;
}
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,51 @@
package snackscription.subscriptionadmin.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import snackscription.subscriptionadmin.dto.AdminDTO;
import snackscription.subscriptionadmin.model.AdminSubscription;
import snackscription.subscriptionadmin.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RestController
@RequestMapping("/admin")
@CrossOrigin(origins = "http://localhost:3000")
public class AdminController {
@Autowired
private AdminService service;

@GetMapping("")
public String adminPage(Model model) {
AdminSubscription adminSubscription = new AdminSubscription();
model.addAttribute("adminSubscription", adminSubscription);
return "admin";
private final AdminService adminService;

public AdminController(AdminService adminService) {
this.adminService = adminService;
}

@PostMapping("/create")
public String createAdminSubscription(@ModelAttribute AdminSubscription adminSubscription, Model model) {
service.create(adminSubscription);
return "redirect:admin";
public ResponseEntity<AdminSubscription> create(@RequestBody AdminDTO adminDTO) {
AdminSubscription adminSubscription = adminService.create(adminDTO);
return new ResponseEntity<>(adminSubscription, HttpStatus.CREATED);
}

@GetMapping("/list")
public String subscriptionListPage(Model model){
List<AdminSubscription> adminSubscriptions = service.findAll();
model.addAttribute("adminSubscriptions", adminSubscriptions);
return "adminSubscriptionList";
public ResponseEntity<List<AdminDTO>> findAll() {
List<AdminDTO> adminDTOList = adminService.findAll();
return new ResponseEntity<>(adminDTOList, HttpStatus.OK);
}

@GetMapping("/{subscriptionId}")
public ResponseEntity<AdminDTO> findById(@PathVariable String subscriptionId) {
AdminDTO adminDTO = adminService.findById(subscriptionId);
return new ResponseEntity<>(adminDTO, HttpStatus.OK);
}

@PutMapping("/update")
public ResponseEntity<AdminSubscription> update(@RequestBody AdminDTO adminDTO) {
AdminSubscription adminSubscription = adminService.update(adminDTO);
return new ResponseEntity<>(adminSubscription, HttpStatus.OK);
}

@DeleteMapping("/{subscriptionId}")
public ResponseEntity<Void> delete(@PathVariable String subscriptionId) {
adminService.delete(subscriptionId);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}
27 changes: 27 additions & 0 deletions src/main/java/snackscription/subscriptionadmin/dto/AdminDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package snackscription.subscriptionadmin.dto;

import lombok.*;


@Data
@AllArgsConstructor
@NoArgsConstructor
@Getter @Setter
public class AdminDTO {
private String subscriptionId;
private String subscriptionType;
private String subscriberName;
private String subscriberId;
private String subscriptionBoxId;
private String subscriptionStatus;
private String uniqueCode;

public AdminDTO(String subscriptionId, String uniqueCode, String subscriberName, String subscriberId, String subscriptionBoxId, String subscriptionStatus) {
this.subscriptionId = subscriptionId;
this.uniqueCode = uniqueCode;
this.subscriberName = subscriberName;
this.subscriberId = subscriberId;
this.subscriptionBoxId = subscriptionBoxId;
this.subscriptionStatus = subscriptionStatus;
}
}
42 changes: 42 additions & 0 deletions src/main/java/snackscription/subscriptionadmin/dto/DTOMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package snackscription.subscriptionadmin.dto;

import org.springframework.stereotype.Component;
import snackscription.subscriptionadmin.factory.AdminSubscriptionFactory;
import snackscription.subscriptionadmin.model.AdminSubscription;

import java.util.Optional;

@Component
public class DTOMapper {

public static AdminDTO convertModelToDto(AdminSubscription adminSubscription) {
return new AdminDTO(
adminSubscription.getSubscriptionId(),
adminSubscription.getUniqueCode(),
adminSubscription.getSubscriberName(),
adminSubscription.getSubscriberId(),
adminSubscription.getSubscriptionBoxId(),
adminSubscription.getSubscriptionStatus()
);
}

public static AdminSubscription convertDTOtoModel(AdminDTO adminDTO) {
String subscriptionType = adminDTO.getSubscriptionType();
String subscriberName = adminDTO.getSubscriberName();
String subscriberId = adminDTO.getSubscriberId();
String subscriptionBoxId = adminDTO.getSubscriptionBoxId();
return new AdminSubscriptionFactory().create(subscriptionType, subscriberName, subscriberId, subscriptionBoxId);
}

public static AdminSubscription updateAdminSubscription(AdminSubscription adminSubscription, AdminDTO adminDTO) {
Optional.ofNullable(adminDTO.getSubscriptionStatus()).ifPresent(adminSubscription::setSubscriptionStatus);
return adminSubscription;
}
}







This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public AdminSubscription create() {
return new AdminSubscription();
}

public AdminSubscription create(String subscriberName, String subscriberId, String uniqueCode, String subscriptionBoxId, String subscriptionStatus) {
return new AdminSubscription(subscriberName, subscriberId, uniqueCode, subscriptionBoxId, subscriptionStatus);
public AdminSubscription create(String subscriptionType, String subscriberName, String subscriberId, String subscriptionBoxId) {
return new AdminSubscription(subscriptionType, subscriberName, subscriberId, subscriptionBoxId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

public interface Factory <F> {
F create();

F create(String subscriptionType, String subscriberName, String subscriberId, String subscriptionBoxId);
}
Original file line number Diff line number Diff line change
@@ -1,39 +1,67 @@
package snackscription.subscriptionadmin.model;

import jakarta.persistence.*;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import snackscription.subscriptionadmin.enums.SubscriptionStatus;

import java.util.UUID;

@Getter @Setter
@Entity
@Getter
@Setter
@Table(name = "admin_subscription")
public class AdminSubscription {
@Id
String subscriptionId;

@Column(name = "unique_code", unique = true)
String uniqueCode;

@Column(name = "subscription_type")
String subscriptionType;

@Column(name = "subscriber_name")
String subscriberName;

@Column(name = "subscriber_id")
String subscriberId;
String uniqueCode;
String subscriptionId;

@Column(name = "subscription_box_id")
String subscriptionBoxId;

@Column(name = "subscription_status")
String subscriptionStatus;

public AdminSubscription() {
this.subscriptionId = UUID.randomUUID().toString();
}

public AdminSubscription(String subscriberName, String subscriberId, String uniqueCode, String subscriptionBoxId, String subscriptionStatus) {
public AdminSubscription(String subscriptionType, String subscriberName, String subscriberId, String subscriptionBoxId) {
this.subscriptionId = UUID.randomUUID().toString();

this.subscriberName = subscriberName;
this.subscriberId = subscriberId;

if (!uniqueCode.startsWith("MTH-") && !uniqueCode.startsWith("QTR-") && !uniqueCode.startsWith("SAA-")) {
throw new IllegalArgumentException("Invalid unique code");
}
this.subscriptionType = subscriptionType;
this.setUniqueCode(subscriptionType);

this.uniqueCode = uniqueCode;
this.subscriptionBoxId = subscriptionBoxId;
this.getSubscriptionStatus();
this.setSubscriptionStatus(subscriptionStatus);
this.setSubscriptionStatus(SubscriptionStatus.PENDING.getValue());
}

public void setUniqueCode(String subscriptionType) {
String prefix = switch (subscriptionType) {
case "MONTHLY" -> "MTH-";
case "QUARTERLY" -> "QTR-";
case "SEMI-ANNUAL" -> "SAA-";
default -> throw new IllegalArgumentException("Invalid type");
};

String randomPart = UUID.randomUUID().toString();
randomPart = randomPart.replace("-", "").toUpperCase().substring(0, 16);
this.uniqueCode = prefix + randomPart;
}

public void setSubscriptionStatus(String subscriptionStatus) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,47 @@
package snackscription.subscriptionadmin.repository;

import jakarta.persistence.EntityManager;
import jakarta.persistence.TypedQuery;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import snackscription.subscriptionadmin.model.AdminSubscription;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;

@Repository
public class AdminRepository {
private List<AdminSubscription> subscriptionData = new ArrayList<>();
@Autowired
private EntityManager entityManager;

@Transactional
public AdminSubscription create(AdminSubscription adminSubscription) {
subscriptionData.add(adminSubscription);
entityManager.persist(adminSubscription);
return adminSubscription;
}

public Iterator<AdminSubscription> findAll() {
return subscriptionData.iterator();
@Transactional
public List<AdminSubscription> findAll() {
String jpql = "SELECT a FROM AdminSubscription a";
TypedQuery<AdminSubscription> query = entityManager.createQuery(jpql, AdminSubscription.class);
return query.getResultList();
}

@Transactional
public Optional<AdminSubscription> findById(String subscriptionId) {
AdminSubscription adminSubscription = entityManager.find(AdminSubscription.class, subscriptionId);
return Optional.ofNullable(adminSubscription);
}

@Transactional
public AdminSubscription update(AdminSubscription adminSubscription) {
return entityManager.merge(adminSubscription);
}

@Transactional
public void delete(String subscriptionId) {
AdminSubscription adminSubscription = findById(subscriptionId).orElseThrow(() -> new IllegalArgumentException("Subscription not found"));
entityManager.remove(adminSubscription);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package snackscription.subscriptionadmin.service;

import snackscription.subscriptionadmin.dto.AdminDTO;
import snackscription.subscriptionadmin.model.AdminSubscription;

import java.util.List;

public interface AdminService {
public AdminSubscription create(AdminSubscription adminSubscription);
public List<AdminSubscription> findAll();
AdminSubscription create(AdminDTO adminDTO);
List<AdminDTO> findAll();
AdminDTO findById(String subscriptionId);
AdminSubscription update(AdminDTO adminDTO);
void delete(String subscriptionId);
}
Loading

0 comments on commit d231fc0

Please sign in to comment.