-
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.
feature: Submit building subscription
- Loading branch information
Tran Gia Bao
committed
Feb 25, 2025
1 parent
4ddb062
commit c899364
Showing
19 changed files
with
401 additions
and
1 deletion.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
sep490-enterprise/src/main/java/enterprise/dtos/CreditConvertRatioDTO.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,8 @@ | ||
package enterprise.dtos; | ||
|
||
import enterprise.entities.CreditConvertType; | ||
|
||
import java.util.UUID; | ||
|
||
public record CreditConvertRatioDTO(UUID id, int version, double ratio, CreditConvertType convertType) { | ||
} |
15 changes: 15 additions & 0 deletions
15
sep490-enterprise/src/main/java/enterprise/dtos/SubscribeRequestDTO.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,15 @@ | ||
package enterprise.dtos; | ||
|
||
import enterprise.entities.TransactionType; | ||
import jakarta.validation.constraints.Min; | ||
|
||
import java.util.UUID; | ||
|
||
public record SubscribeRequestDTO( | ||
UUID buildingId, | ||
@Min(1) int months, | ||
@Min(100) int numberOfDevices, | ||
double monthRatio, | ||
double deviceRatio, | ||
TransactionType type) { | ||
} |
26 changes: 26 additions & 0 deletions
26
sep490-enterprise/src/main/java/enterprise/entities/CreditConvertRatioEntity.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 enterprise.entities; | ||
|
||
import commons.springfw.impl.entities.AbstractAuditableEntity; | ||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.EnumType; | ||
import jakarta.persistence.Enumerated; | ||
import jakarta.persistence.Table; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
@Entity | ||
@Table(name = "credit_convert_ratio") | ||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
public class CreditConvertRatioEntity extends AbstractAuditableEntity { | ||
|
||
@Column(name = "ratio") | ||
private double ratio; | ||
|
||
@Enumerated(EnumType.STRING) | ||
@Column(name = "convert_type", unique = true) | ||
private CreditConvertType convertType; | ||
} |
6 changes: 6 additions & 0 deletions
6
sep490-enterprise/src/main/java/enterprise/entities/CreditConvertType.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,6 @@ | ||
package enterprise.entities; | ||
|
||
public enum CreditConvertType { | ||
MONTH, | ||
DEVICE | ||
} |
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
6 changes: 6 additions & 0 deletions
6
sep490-enterprise/src/main/java/enterprise/entities/TransactionType.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,6 @@ | ||
package enterprise.entities; | ||
|
||
public enum TransactionType { | ||
NEW_PURCHASE, | ||
UPGRADE | ||
} |
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
15 changes: 15 additions & 0 deletions
15
sep490-enterprise/src/main/java/enterprise/mappers/CreditConvertRatioMapper.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,15 @@ | ||
package enterprise.mappers; | ||
|
||
import enterprise.dtos.CreditConvertRatioDTO; | ||
import enterprise.entities.CreditConvertRatioEntity; | ||
import org.mapstruct.Mapper; | ||
import org.mapstruct.MappingConstants; | ||
|
||
@Mapper(componentModel = MappingConstants.ComponentModel.SPRING) | ||
public interface CreditConvertRatioMapper { | ||
|
||
CreditConvertRatioDTO toDTO(CreditConvertRatioEntity entity); | ||
|
||
CreditConvertRatioEntity toEntity(CreditConvertRatioDTO dto); | ||
|
||
} |
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
11 changes: 11 additions & 0 deletions
11
sep490-enterprise/src/main/java/enterprise/repositories/CreditConvertRatioRepository.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,11 @@ | ||
package enterprise.repositories; | ||
|
||
import enterprise.entities.CreditConvertRatioEntity; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.UUID; | ||
|
||
@Repository | ||
public interface CreditConvertRatioRepository extends JpaRepository<CreditConvertRatioEntity, UUID> { | ||
} |
14 changes: 13 additions & 1 deletion
14
sep490-enterprise/src/main/java/enterprise/repositories/SubscriptionRepository.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,8 +1,20 @@ | ||
package enterprise.repositories; | ||
|
||
import commons.springfw.impl.repositories.AbstractBaseRepository; | ||
import enterprise.entities.BuildingEntity; | ||
import enterprise.entities.SubscriptionEntity; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
|
||
import java.time.LocalDate; | ||
import java.util.List; | ||
|
||
public interface SubscriptionRepository extends AbstractBaseRepository<SubscriptionEntity> { | ||
|
||
@Query(value = """ | ||
select s from SubscriptionEntity s | ||
where s.startDate <= :now | ||
and s.endDate >= :now | ||
""") | ||
List<SubscriptionEntity> findAllValidSubscriptions(@Param("now") LocalDate now); | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
sep490-enterprise/src/main/java/enterprise/rest/SubscriptionController.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,44 @@ | ||
package enterprise.rest; | ||
|
||
|
||
import enterprise.dtos.CreditConvertRatioDTO; | ||
import enterprise.dtos.SubscribeRequestDTO; | ||
import enterprise.mappers.CreditConvertRatioMapper; | ||
import enterprise.services.SubscriptionService; | ||
import green_buildings.commons.api.security.UserRole; | ||
import jakarta.annotation.security.RolesAllowed; | ||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequestMapping("/api/subscription") | ||
@RequiredArgsConstructor | ||
public class SubscriptionController { | ||
|
||
private final SubscriptionService subscriptionService; | ||
private final CreditConvertRatioMapper creditConvertRatioMapper; | ||
|
||
|
||
@GetMapping("/convert-ratio") | ||
public ResponseEntity<List<CreditConvertRatioDTO>> getCreditConvertRatio() { | ||
List<CreditConvertRatioDTO> list = subscriptionService.getCreditConvertRatios().stream().map(creditConvertRatioMapper::toDTO).toList(); | ||
return ResponseEntity.ok(list); | ||
} | ||
|
||
@PostMapping | ||
@RolesAllowed(value = {UserRole.RoleNameConstant.ENTERPRISE_OWNER}) | ||
public ResponseEntity<Void> subscribe(@RequestBody @Valid SubscribeRequestDTO request) { | ||
subscriptionService.subscribe(request); | ||
return ResponseEntity.status(HttpStatus.CREATED).build(); | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
sep490-enterprise/src/main/java/enterprise/services/SubscriptionService.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 enterprise.services; | ||
|
||
import enterprise.dtos.SubscribeRequestDTO; | ||
import enterprise.entities.CreditConvertRatioEntity; | ||
import jakarta.validation.Valid; | ||
|
||
import java.util.List; | ||
|
||
public interface SubscriptionService { | ||
List<CreditConvertRatioEntity> getCreditConvertRatios(); | ||
|
||
void subscribe(@Valid SubscribeRequestDTO request); | ||
} |
Oops, something went wrong.