-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from ADPRO-C11/staging
Staging
- Loading branch information
Showing
21 changed files
with
525 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/main/java/snackscription/subscriptionadmin/config/AsyncConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
54 changes: 34 additions & 20 deletions
54
src/main/java/snackscription/subscriptionadmin/controller/AdminController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
27
src/main/java/snackscription/subscriptionadmin/dto/AdminDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
42
src/main/java/snackscription/subscriptionadmin/dto/DTOMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
14 changes: 0 additions & 14 deletions
14
src/main/java/snackscription/subscriptionadmin/factory/AdminRepositoryFactory.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 38 additions & 10 deletions
48
src/main/java/snackscription/subscriptionadmin/model/AdminSubscription.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 31 additions & 6 deletions
37
src/main/java/snackscription/subscriptionadmin/repository/AdminRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
8 changes: 6 additions & 2 deletions
8
src/main/java/snackscription/subscriptionadmin/service/AdminService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.