-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4c9d508
commit 702197b
Showing
38 changed files
with
1,019 additions
and
23 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
51 changes: 51 additions & 0 deletions
51
hla-product-ordering/backend/src/main/java/com/amigoscode/api/email/EmailController.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,51 @@ | ||
package com.amigoscode.api.email; | ||
|
||
|
||
import com.amigoscode.appservices.EmailApplicationService; | ||
import com.amigoscode.domain.email.Email; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RequiredArgsConstructor | ||
@RestController | ||
@RequestMapping(path = "/api/v1/emails", | ||
produces = "application/json", | ||
consumes = "application/json" | ||
) | ||
class EmailController { | ||
private final EmailApplicationService emailService; | ||
|
||
private final EmailDtoMapper emailMapper; | ||
|
||
private final PageEmailDtoMapper pageEmailDtoMapper; | ||
|
||
@GetMapping( path = "/{emailId}") | ||
public ResponseEntity<EmailDto> getEmail(@PathVariable Integer emailId) { | ||
Email email = emailService.findById(emailId); | ||
return ResponseEntity | ||
.ok(emailMapper.toDto(email)); | ||
} | ||
|
||
@GetMapping | ||
public ResponseEntity<PageEmailDto> getEmails( | ||
@RequestParam(defaultValue = "0") int page, | ||
@RequestParam(defaultValue = "3") int size | ||
) { | ||
Pageable pageable = PageRequest.of(page, size); | ||
PageEmailDto pageEmails = pageEmailDtoMapper.toPageDto(emailService.findAll(pageable)); | ||
|
||
return ResponseEntity.ok(pageEmails); | ||
} | ||
|
||
@PostMapping | ||
public ResponseEntity<EmailDto> sendEmail(@RequestBody EmailDto dto){ | ||
|
||
Email sentEmail = emailService.send(emailMapper.toDomain(dto)); | ||
Email savedEmail = emailService.save(sentEmail); | ||
return ResponseEntity | ||
.ok(emailMapper.toDto(savedEmail)); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
hla-product-ordering/backend/src/main/java/com/amigoscode/api/email/EmailDto.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,13 @@ | ||
package com.amigoscode.api.email; | ||
|
||
import java.time.ZonedDateTime; | ||
import java.util.List; | ||
|
||
public record EmailDto( | ||
Integer id, | ||
Integer providerId, | ||
ZonedDateTime createdAt, | ||
Integer userId, | ||
String content, | ||
List<Integer> orders | ||
) {} |
13 changes: 13 additions & 0 deletions
13
hla-product-ordering/backend/src/main/java/com/amigoscode/api/email/EmailDtoMapper.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,13 @@ | ||
package com.amigoscode.api.email; | ||
|
||
import com.amigoscode.api.email.EmailDto; | ||
import com.amigoscode.domain.email.Email; | ||
import org.mapstruct.Mapper; | ||
|
||
@Mapper(componentModel = "spring") | ||
public interface EmailDtoMapper { | ||
|
||
EmailDto toDto(Email domain); | ||
|
||
Email toDomain(EmailDto dto); | ||
} |
13 changes: 13 additions & 0 deletions
13
hla-product-ordering/backend/src/main/java/com/amigoscode/api/email/PageEmailDto.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,13 @@ | ||
package com.amigoscode.api.email; | ||
|
||
import com.amigoscode.api.order.OrderDto; | ||
|
||
import java.util.List; | ||
|
||
public record PageEmailDto( | ||
List<EmailDto> emails, | ||
Integer currentPage, | ||
Integer totalPages, | ||
Long totalElements | ||
) | ||
{} |
26 changes: 26 additions & 0 deletions
26
hla-product-ordering/backend/src/main/java/com/amigoscode/api/email/PageEmailDtoMapper.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,26 @@ | ||
package com.amigoscode.api.email; | ||
|
||
import com.amigoscode.api.email.EmailDto; | ||
import com.amigoscode.api.email.PageEmailDto; | ||
import com.amigoscode.domain.email.Email; | ||
import com.amigoscode.domain.email.PageEmail; | ||
import org.mapstruct.IterableMapping; | ||
import org.mapstruct.Mapper; | ||
import org.mapstruct.Mapping; | ||
import org.mapstruct.Named; | ||
|
||
import java.util.List; | ||
|
||
@Mapper(componentModel = "spring") | ||
public interface PageEmailDtoMapper { | ||
|
||
@Mapping(target = "emails", qualifiedByName = "toEmailDtoList") | ||
PageEmailDto toPageDto(PageEmail domain); | ||
|
||
@Named("toEmailDtoList") | ||
@IterableMapping(qualifiedByName = "emailToEmailDto") | ||
List<EmailDto> toListDto(List<Email> emails); | ||
|
||
@Named("emailToEmailDto") | ||
EmailDto toDto(Email domain); | ||
} |
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
33 changes: 33 additions & 0 deletions
33
...ct-ordering/backend/src/main/java/com/amigoscode/appservices/EmailApplicationService.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,33 @@ | ||
package com.amigoscode.appservices; | ||
|
||
import com.amigoscode.domain.email.Email; | ||
import com.amigoscode.domain.email.EmailNotFoundException; | ||
import com.amigoscode.domain.email.EmailService; | ||
import com.amigoscode.domain.email.PageEmail; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class EmailApplicationService { | ||
private final EmailService emailService; | ||
|
||
public Email findById(Integer id){ | ||
return emailService.findById(id); | ||
} | ||
|
||
public PageEmail findAll(Pageable pageable) { return emailService.findAll(pageable); } | ||
|
||
@Transactional | ||
public Email save(Email email) { | ||
return emailService.save(email); | ||
} | ||
|
||
public Email send(Email email) { | ||
return emailService.send(email); | ||
} | ||
|
||
|
||
} |
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
24 changes: 24 additions & 0 deletions
24
hla-product-ordering/backend/src/main/java/com/amigoscode/domain/email/Email.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,24 @@ | ||
package com.amigoscode.domain.email; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.ToString; | ||
|
||
import java.time.ZonedDateTime; | ||
import java.util.List; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@ToString | ||
@EqualsAndHashCode | ||
public class Email { | ||
|
||
private Integer id; | ||
private Integer providerId; | ||
private ZonedDateTime createdAt; | ||
private Integer userId; | ||
private String content; | ||
private List<Integer> orders; | ||
|
||
} |
4 changes: 4 additions & 0 deletions
4
...dering/backend/src/main/java/com/amigoscode/domain/email/EmailAlreadyExistsException.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,4 @@ | ||
package com.amigoscode.domain.email; | ||
|
||
public class EmailAlreadyExistsException extends RuntimeException { | ||
} |
4 changes: 4 additions & 0 deletions
4
...ct-ordering/backend/src/main/java/com/amigoscode/domain/email/EmailNotFoundException.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,4 @@ | ||
package com.amigoscode.domain.email; | ||
|
||
public class EmailNotFoundException extends RuntimeException{ | ||
} |
17 changes: 17 additions & 0 deletions
17
hla-product-ordering/backend/src/main/java/com/amigoscode/domain/email/EmailRepository.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,17 @@ | ||
package com.amigoscode.domain.email; | ||
|
||
import com.amigoscode.domain.email.Email; | ||
import com.amigoscode.domain.email.PageEmail; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
import java.util.Optional; | ||
|
||
public interface EmailRepository { | ||
Optional<Email> findById(Integer id); | ||
|
||
PageEmail findAll(Pageable pageable); | ||
|
||
Email save(Email email); | ||
|
||
|
||
} |
5 changes: 5 additions & 0 deletions
5
hla-product-ordering/backend/src/main/java/com/amigoscode/domain/email/EmailSender.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,5 @@ | ||
package com.amigoscode.domain.email; | ||
|
||
public interface EmailSender { | ||
Email send(Email email); | ||
} |
65 changes: 65 additions & 0 deletions
65
hla-product-ordering/backend/src/main/java/com/amigoscode/domain/email/EmailService.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,65 @@ | ||
package com.amigoscode.domain.email; | ||
|
||
import com.amigoscode.domain.order.Order; | ||
import com.amigoscode.domain.order.OrderService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
import java.util.List; | ||
|
||
@RequiredArgsConstructor | ||
public class EmailService { | ||
private final EmailRepository emailRepository; | ||
private final EmailSender emailSender; | ||
private final OrderService orderService; | ||
|
||
|
||
public Email findById(Integer id){ | ||
Email email = emailRepository.findById(id). | ||
orElseThrow(EmailNotFoundException::new); | ||
List<Order> orders = orderService.findByEmailId(id); | ||
email.setOrders(orders.stream().map(Order::getId).toList()); | ||
return email; | ||
} | ||
|
||
public PageEmail findAll(Pageable pageable) { | ||
final PageEmail pageEmail = emailRepository.findAll(pageable); | ||
for (Email email : pageEmail.getEmails()) { | ||
List<Order> orders = orderService.findByEmailId(email.getId()); | ||
email.setOrders(orders.stream().map(Order::getId).toList()); | ||
} | ||
|
||
return pageEmail; | ||
} | ||
|
||
public Email save(Email email) { | ||
Email savedEmail = emailRepository.save(email); | ||
savedEmail.setOrders(email.getOrders()); | ||
markOrdersAsSentByEmail(savedEmail); | ||
return savedEmail; | ||
} | ||
|
||
public Email send(Email email) { | ||
final String currentContent = email.getContent(); | ||
final List<Integer> ordersWithoutEmailId = email.getOrders() | ||
.stream() | ||
.filter(id -> orderService.findById(id).getEmailId() == null).toList(); | ||
email.setOrders(ordersWithoutEmailId); | ||
// information needed to order products | ||
String orderRelatedContent = email.getOrders().toString(); | ||
email.setContent(currentContent + " " + orderRelatedContent); | ||
|
||
emailSender.send(email); | ||
|
||
return email; | ||
} | ||
|
||
private void markOrdersAsSentByEmail (Email email) { | ||
for (Integer id: email.getOrders()) { | ||
Order order = orderService.findById(id); | ||
order.setEmailId(email.getId()); | ||
orderService.update(order); | ||
} | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
hla-product-ordering/backend/src/main/java/com/amigoscode/domain/email/PageEmail.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,16 @@ | ||
package com.amigoscode.domain.email; | ||
|
||
|
||
import lombok.Value; | ||
|
||
import java.io.Serializable; | ||
import java.util.List; | ||
|
||
@Value | ||
public class PageEmail implements Serializable { | ||
|
||
List<Email> emails; | ||
Integer currentPage; | ||
Integer totalPages; | ||
Long totalElements; | ||
} |
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
Oops, something went wrong.