FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

I added coupon feature to my rest api . · bekkaoui99/Ecommerce-REST-API@2592dc1 · GitHub

Repository navigation

Commit 2592dc1

Browse files
committed
I added coupon feature to my rest api .
1 parent 88ff213 commit 2592dc1

15 files changed

Lines changed: 330 additions & 7 deletions

File tree

‎src/main/java/com/hamzabekkaoui/ecommerceapplication/controller/CategoryController.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public Page<CategoryResponse> getPageOfCategory(
2222
@RequestParam(name = "pageNumber" , defaultValue = "0") int pageNumber ,
2323
@RequestParam(name = "pageSize" , defaultValue = "0") int pageSize
2424
){
25-
return categoryService.pageOfData(pageNumber, pageSize);
25+
return categoryService.getAllAsPage(pageNumber, pageSize);
2626

2727
}
2828

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.hamzabekkaoui.ecommerceapplication.controller;
2+
3+
import com.hamzabekkaoui.ecommerceapplication.dto.request.CouponRequest;
4+
import com.hamzabekkaoui.ecommerceapplication.dto.response.CouponResponse;
5+
import com.hamzabekkaoui.ecommerceapplication.service.CouponService;
6+
import org.springframework.data.domain.Page;
7+
import org.springframework.security.access.prepost.PreAuthorize;
8+
import org.springframework.web.bind.annotation.*;
9+
10+
import java.util.List;
11+
12+
@RestController
13+
@RequestMapping("api/v1/coupons")
14+
public class CouponController {
15+
16+
17+
private final CouponService couponService;
18+
19+
20+
public CouponController(CouponService couponService) {
21+
this.couponService = couponService;
22+
}
23+
24+
@PostMapping
25+
@PreAuthorize("hasAuthority('ADMIN')")
26+
public CouponResponse createCoupon(@RequestBody CouponRequest couponRequest){
27+
return this.couponService.create(couponRequest);
28+
}
29+
30+
@PutMapping("/{couponId}")
31+
@PreAuthorize("hasAuthority('ADMIN')")
32+
public CouponResponse updateCoupon(@RequestBody CouponRequest couponRequest, @PathVariable Integer couponId){
33+
return this.couponService.update(couponRequest , couponId);
34+
}
35+
36+
37+
@DeleteMapping("/{couponId}")
38+
@PreAuthorize("hasAuthority('ADMIN')")
39+
public boolean deleteCoupon(@PathVariable Integer couponId){
40+
return this.couponService.delete(couponId);
41+
}
42+
43+
44+
@GetMapping("/list")
45+
@PreAuthorize("hasAuthority('ADMIN')")
46+
public List<CouponResponse> getAllCouponsAsList(){
47+
return this.couponService.getAllAsList();
48+
}
49+
50+
51+
@GetMapping("/page")
52+
@PreAuthorize("hasAuthority('ADMIN')")
53+
public Page<CouponResponse> getAllCouponsAsPage(
54+
@RequestParam(name = "pageNumber", defaultValue = "0") int pageNumber,
55+
@RequestParam(name = "pageSize", defaultValue = "9") int pageSize
56+
){
57+
return this.couponService.getAllAsPage(pageNumber, pageSize);
58+
}
59+
60+
@GetMapping("/code")
61+
@PreAuthorize("hasAuthority('ADMIN')")
62+
public CouponResponse getAllCouponsAsPage(
63+
@RequestParam(name = "couponCode") String couponCode
64+
){
65+
return this.couponService.getCouponByCode(couponCode);
66+
}
67+
68+
69+
}

‎src/main/java/com/hamzabekkaoui/ecommerceapplication/controller/OrderController.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public Page<OrderResponse> getAllOrders(
3030
@RequestParam(name = "pageNumber" , defaultValue = "0") int pageNumber ,
3131
@RequestParam(name = "pageSize" , defaultValue = "0") int pageSize
3232
){
33-
return orderService.pageOfData(pageNumber, pageSize);
33+
return orderService.getAllAsPage(pageNumber, pageSize);
3434

3535
}
3636

‎src/main/java/com/hamzabekkaoui/ecommerceapplication/controller/ProductController.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public Page<ProductResponse> getAllProducts(
2525
@RequestParam(name = "pageNumber" , defaultValue = "0") int pageNumber ,
2626
@RequestParam(name = "pageSize" , defaultValue = "0") int pageSize
2727
){
28-
return productService.pageOfData(pageNumber, pageSize);
28+
return productService.getAllAsPage(pageNumber, pageSize);
2929

3030
}
3131

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.hamzabekkaoui.ecommerceapplication.dto.request;
2+
3+
import lombok.Builder;
4+
5+
import java.time.LocalDateTime;
6+
7+
8+
@Builder
9+
public record CouponRequest(
10+
Double discountPercentage,
11+
LocalDateTime expirationDate
12+
) {
13+
14+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.hamzabekkaoui.ecommerceapplication.dto.response;
2+
3+
import lombok.Builder;
4+
5+
import java.time.LocalDateTime;
6+
7+
8+
@Builder
9+
public record CouponResponse(
10+
Integer id,
11+
String code,
12+
Double discountPercentage,
13+
LocalDateTime expirationDate,
14+
boolean isActive
15+
) {
16+
17+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.hamzabekkaoui.ecommerceapplication.entity;
2+
3+
import jakarta.persistence.Entity;
4+
import jakarta.persistence.GeneratedValue;
5+
import jakarta.persistence.GenerationType;
6+
import jakarta.persistence.Id;
7+
import lombok.*;
8+
9+
import java.time.LocalDateTime;
10+
11+
12+
@NoArgsConstructor
13+
@AllArgsConstructor
14+
@Getter
15+
@Setter
16+
@Builder
17+
@Entity
18+
public class Coupon {
19+
20+
21+
@Id
22+
@GeneratedValue(strategy = GenerationType.IDENTITY)
23+
private Integer id;
24+
25+
private String code;
26+
27+
private Double discountPercentage;
28+
29+
private LocalDateTime expirationDate;
30+
31+
private Boolean isActive;
32+
33+
34+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.hamzabekkaoui.ecommerceapplication.mapper;
2+
3+
import com.hamzabekkaoui.ecommerceapplication.dto.request.CouponRequest;
4+
import com.hamzabekkaoui.ecommerceapplication.dto.response.CouponResponse;
5+
import com.hamzabekkaoui.ecommerceapplication.entity.Coupon;
6+
import org.springframework.stereotype.Service;
7+
8+
@Service
9+
public class CouponMapper {
10+
11+
public CouponResponse couponToCouponResponse(Coupon coupon){
12+
return CouponResponse.builder()
13+
.id(coupon.getId())
14+
.code(coupon.getCode())
15+
.discountPercentage(coupon.getDiscountPercentage())
16+
.expirationDate(coupon.getExpirationDate())
17+
.isActive(coupon.getIsActive())
18+
.build();
19+
}
20+
21+
public Coupon couponRequestToCoupon(CouponRequest couponRequest){
22+
return Coupon.builder()
23+
.discountPercentage(couponRequest.discountPercentage())
24+
.expirationDate(couponRequest.expirationDate())
25+
.build();
26+
}
27+
28+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.hamzabekkaoui.ecommerceapplication.repository;
2+
3+
import com.hamzabekkaoui.ecommerceapplication.entity.Coupon;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
import java.util.Optional;
7+
8+
public interface CouponRepository extends JpaRepository<Coupon, Integer> {
9+
10+
Optional<Coupon> findByCode(String couponCode);
11+
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.hamzabekkaoui.ecommerceapplication.service;
2+
3+
4+
import com.hamzabekkaoui.ecommerceapplication.dto.request.CouponRequest;
5+
import com.hamzabekkaoui.ecommerceapplication.dto.response.CouponResponse;
6+
7+
8+
public interface CouponService extends CrudService<CouponRequest, CouponResponse, Integer>{
9+
10+
CouponResponse getCouponByCode(String couponCode);
11+
12+
}

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL