0% found this document useful (0 votes)
9 views4 pages

Controller

Uploaded by

[L]Jinesh pande
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Controller

Uploaded by

[L]Jinesh pande
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Controller:

@RequestMapping(method = GET, value = "/all-subscriptions/{subscriptionId}" )


public ResponseEntity<ApiResponse> getSubscriptionBySubId(
@PathVariable(value = "subscriptionId") Long subscriptionId,
HttpServletRequest request) {
User principal = (User)
request.getAttribute(ApiCommonEnums.PRINCIPAL.getValue());
List<Map<String, Object>> responseBodyList =
subscriptionServiceV2.getSubscriptionBySubId(subscriptionId);
return new ResponseEntity<>(new ApiResponseSuccess(responseBodyList),
HttpStatus.OK);
}

SubscriptionServiceV2:
public List<Map<String, Object>> getSubscriptionBySubId(Long subscriptionId) {
List<Subscription> subscriptions =
subscriptionRepository.getSubscriptionBySubId(subscriptionId);
return subscriptions.stream()
.map(subscription -> {
Map<String, Object> response = new LinkedHashMap<>();
response.put("id", subscription.getId());
response.put("companyCount",
subscription.getAllCompanyCountById() + "/" + subscription.getCompanyCount());
Map<String, Object> planDetails = new LinkedHashMap<>();
Plan plan = subscription.getPlan();
planDetails.put("name", plan != null ? plan.getName() : null);
planDetails.put("uniqueName", plan != null ?
plan.getUniqueName() : null);
response.put("planDetails", planDetails);
response.put("balance", subscription.getBalance());
response.put("dueAmount", subscription.getDueAmount());
response.put("expiry", subscription.getExpiry() != null ?
subscription.getExpiry().toString(DateCommonEnum.DATE_COMMON_FORMAT.getValue()) :
null);
response.put("renewDate", subscription.getRenewDate() != null ?
subscription.getRenewDate().toString(DateCommonEnum.DATE_COMMON_FORMAT.getValue())
: null);
response.put("duration", subscription.getDuration() != null ?
subscription.getDuration().toString() : null);
response.put("startedAt", subscription.getStartedAt() != null ?
subscription.getStartedAt().toString(DateCommonEnum.DATE_COMMON_FORMAT.getValue())
: null);
SubscriptionDiscount subscriptionDiscount =
subscription.getSubscriptionDiscount();
Map<String, Object> discountDetails = new LinkedHashMap<>();
discountDetails.put("name", subscriptionDiscount != null ?
subscriptionDiscount.getName() : null);
discountDetails.put("value", subscriptionDiscount != null ?
subscriptionDiscount.getDiscountValue() : null);
response.put("subscriptionDiscount", discountDetails);
SubscriptionPromoCode promoCode = subscription.getPromoCode();
Map<String, Object> promoCodeDetails = new LinkedHashMap<>();
promoCodeDetails.put("expiryDate", promoCode != null ?
promoCode.getExpiryDate().toString(DateCommonEnum.DATE_COMMON_FORMAT.getValue()) :
null);
response.put("promoCode", promoCodeDetails);
response.put("autoRenewable", true);
response.put("status", subscription.getStatus());
User user = subscription.getUser();
Map<String, Object> userDetails = new LinkedHashMap<>();
userDetails.put("name", user != null ? user.getName() : null);
response.put("user", userDetails);
return response;
})
.collect(Collectors.toList());
}

SubscriptionRepository:
@Query(nativeQuery = true)
List<Subscription> getSubscriptionBySubId(@Param("id") Long id);

subscription: @NamedNativeQuery(
name = "Subscription.getSubscriptionBySubId",
query = "SELECT s.id, p.plan_name, p.unique_name " +
" get_company_count_by_subscription_id(s.id) AS
company_count_By_Id, s.company_count AS total_company " +
" s.balance, 0 AS due_amount " +
" s.expiry, s.expiry + INTERVAL '1 days' AS
renew_date, s.duration, s.started_at " +
" CASE WHEN s.duration = 'MONTHLY' THEN md.name ELSE
yd.name END as discount_name " +
" CASE WHEN s.duration = 'MONTHLY' THEN
md.discount_value ELSE yd.discount_value END as discount_value " +
" spc.expiry_date AS promo_end_date " +
" spc.archive_status " +
" TRUE auto_renewable, s.status AS Status, u.name " +
"FROM subscription s " +
"JOIN plan p ON s.plan_id = p.id " +
"left JOIN subscription_discount yd ON p.yearly_discount_id
= yd.id " +
"left JOIN subscription_discount md ON
p.monthly_discount_id = md.id " +
"JOIN users u ON u.id = s.user_id " +
"left JOIN subscription_promo_code_plan_mapping spcpm ON
p.id = spcpm.plan_id " +
"left JOIN subscription_promo_code spc ON
spcpm.promo_code_id = spc.id " +
"JOIN company c ON c.subscription_id = s.id where s.id
= :id",
resultSetMapping = "SubscriptionBySubId"
)

@SqlResultSetMapping(name = "SubscriptionBySubId", classes = {


@ConstructorResult(
targetClass = Subscription.class,
columns = {
@ColumnResult(name = "id", type = Long.class),
@ColumnResult(name = "plan_name", type =
String.class),
@ColumnResult(name = "unique_name", type =
String.class),
@ColumnResult(name = "company_count_By_Id", type =
Integer.class ),
@ColumnResult(name = "total_company", type =
Integer.class),
@ColumnResult(name = "balance", type =
BigDecimal.class),
@ColumnResult(name = "due_amount", type =
Integer.class),
@ColumnResult(name = "expiry", type = Date.class),
@ColumnResult(name = "archive_status", type =
ArchiveStatus.class),
@ColumnResult(name = "renew_date", type =
Date.class),
@ColumnResult(name = "duration", type =
DurationType.class),
@ColumnResult(name = "started_at", type =
Date.class),
@ColumnResult(name = "discount_name", type =
String.class),
@ColumnResult(name = "discount_value", type =
BigDecimal.class),
@ColumnResult(name = "promo_end_date", type =
Date.class),
@ColumnResult(name = "auto_renewable", type =
Boolean.class),
@ColumnResult(name = "status", type =
String.class),
@ColumnResult(name = "name", type = String.class)
}
)
})

@Column(name = "company_count_By_Id")
private Integer AllCompanyCountById;
@Column(name = "due_amount")
private Integer dueAmount;
private SubscriptionDiscount subscriptionDiscount;
@Column(name = "auto_renewable")
private Boolean autoRenewable;
@Column(name = "renew_date")
@Type(value = JodaLocalDateUserType.class)
private LocalDate renewDate;
public Subscription(Long id, String planName, String uniqueName, Integer
companyCount, Integer CompanyAllowed, BigDecimal balance, Integer dueAmount,
Date expiry, ArchiveStatus archiveStatus, Date renewDate,
DurationType duration,
Date startedAt, String discountName, BigDecimal
discountValue,
Date promoEndDate, Boolean autoRenewable, String status,
String name) {
this.id = id;
this.plan = new Plan();
this.plan.setName(planName);
this.plan.setUniqueName(uniqueName);
this.AllCompanyCountById = companyCount;
this.companyCount = CompanyAllowed;
this.balance = balance;
this.dueAmount = dueAmount;
this.expiry = new LocalDate(expiry);
this.renewDate = new LocalDate(renewDate);
this.duration = duration;
this.startedAt = new LocalDateTime(startedAt);
this.subscriptionDiscount=new SubscriptionDiscount();
this.subscriptionDiscount.setName(discountName);
this.subscriptionDiscount.setDiscountValue(discountValue);
this.promoCode = new SubscriptionPromoCode();
this.promoCode.setExpiryDate(new LocalDate(promoEndDate));
this.promoCode.setArchiveStatus(archiveStatus);
this.autoRenewable = autoRenewable;
this.status = status;
this.user=new User();
this.user.setName(name);
}

You might also like