I am trying to implement AddtoCart functionality for courses.But when I tried to check on my swagger about there is not displaying expected field which i wanted. Can Anyone check my code please.
Swagger Showing these fields:
{
"additionalProp1": "string",
"additionalProp2": "string",
"additionalProp3": "string"
}
CartController
@RestController
public class AddToCartController {
@Autowired
CartService cartService;
@PostMapping("/addToCart")
public Cart addCartwithCourse(@RequestBody HashMap<String,String> addCartRequest)
{
try {
// String keys[] = {"courseId","userId","qty","price"};
long courseId = Long.parseLong(addCartRequest.get("courseId"));
long userId = Long.parseLong(addCartRequest.get("userId"));
int qty = Integer.parseInt(addCartRequest.get("qty"));
double price = Double.parseDouble(addCartRequest.get("price"));
Cart obj = cartService.addCartbyUserIdAndCourseId(courseId,userId,qty,price);
return obj;
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
Cart Model:
package com.hashedin.hu22.entities;
import org.springframework.data.annotation.CreatedDate;
import javax.persistence.*;
import java.time.Instant;
@Entity
public class Cart {
@Id
long id;
int qty;
double price;
Long user_id;
@CreatedDate
private Instant createdDate;
String CourseName;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name="cr_fid", referencedColumnName = "id")
Course course;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public int getQty() {
return qty;
}
public void setQty(int qty) {
this.qty = qty;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public Long getUser_id() {
return user_id;
}
public void setUser_id(Long user_id) {
this.user_id = user_id;
}
public Instant getCreatedDate() {
return createdDate;
}
public void setCreatedDate(Instant createdDate) {
this.createdDate = createdDate;
}
public String getCourseName() {
return CourseName;
}
public void setCourseName(String courseName) {
CourseName = courseName;
}
public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
}
AddToCartRepo
public interface AddToCartRepo extends JpaRepository<Cart, Long> {
@Query("Select addCart FROM Cart addCart WHERE addCart.course.id= :course_id and addCart.user_id=:user_id")
Optional<Cart> getCartByProductIdAnduserId(@Param("user_id")Long user_id, @Param("course_id")Long course_id);
@Query("Select addCart FROM Cart addCart WHERE addCart.user_id=:user_id")
List<Cart> getCartByuserId(@Param("user_id")Long user_id);
}
CartService
@Service
public class CartServiceImpl implements CartService {
@Autowired
AddToCartRepo addToCartRepo;
@Autowired
CourseSerivce courseSerivce;
@Override
public Cart addCartbyUserIdAndCourseId(long courseId, long userId, int qty, double price) throws Exception
{
try {
if (addToCartRepo.getCartByProductIdAnduserId(userId, courseId).isPresent()) {
throw new Exception("Product is Already Exist");
}
Cart obj = new Cart();
obj.setQty(qty);
obj.setUser_id(userId);
Course course = courseSerivce.getCourseById(courseId);
obj.setCourse(course);
obj.setPrice(price);
return addToCartRepo.save(obj);
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
@Override
public List<Cart> getCartByUserId(long userId){
return addToCartRepo.getCartByuserId(userId);
}
}
I tried to replicate what you are trying to do and I found many ways to avoid getting this problem! I'm going to post one of the way and hope it helps! In CartController instead of using a hashmap use a DTO(Data Transfer Object) instead with all the fields that are necessary as follows!
CartController
@RestController
public class CartController {
@Autowired
CartService cartService;
@PostMapping("/addToCart")
public Cart addCartwithCourse(@RequestBody CartDto addCartRequest)
{
try {
// String keys[] = {"courseId","userId","qty","price"};
long courseId = addCartRequest.getCourseId();
long userId = addCartRequest.getUserId();
int qty = addCartRequest.getQty();
double price = addCartRequest.getPrice();
Cart obj = cartService.addCartbyUserIdAndCourseId(courseId,userId,qty,price);
return obj;
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
And write a DTO as follows
CartDto
package com.SOF.dto;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CartDto {
private long courseId;
private long userId;
private int qty;
private double price;
}
And we will get as follows in swagger
Required body { "courseId": 0, "userId": 0, "qty": 0, "price": 0 }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.