Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,4 @@ public ResponseDto Signup(@RequestBody SignupDto signupDto) throws CustomExcepti
public SignInResponseDto Signup(@RequestBody SignInDto signInDto) throws CustomException {
return userService.signIn(signInDto);
}

// @PostMapping("/updateUser")
// public ResponseDto updateUser(@RequestParam("token") String token, @RequestBody UserUpdateDto userUpdateDto) {
// authenticationService.authenticate(token);
// return userService.updateUser(token, userUpdateDto);
// }


// @PostMapping("/createUser")
// public ResponseDto updateUser(@RequestParam("token") String token, @RequestBody UserCreateDto userCreateDto)
// throws CustomException, AuthenticationFailException {
// authenticationService.authenticate(token);
// return userService.createUser(token, userCreateDto);
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,56 @@
@RestController
@RequestMapping("/wishlist")
public class WishListController {
@Autowired
WishListService wishListService;

@Autowired
AuthenticationService authenticationService;


// save product as wishlist item
@PostMapping("/add")
public ResponseEntity<ApiResponse> addToWishList(@RequestBody Product product,
@RequestParam("token") String token) {
// authenticate the token
authenticationService.authenticate(token);


// find the user

User user = authenticationService.getUser(token);

// save the item in wishlist

WishList wishList = new WishList(user, product);

wishListService.createWishlist(wishList);

ApiResponse apiResponse = new ApiResponse(true, "Added to wishlist");
return new ResponseEntity<>(apiResponse, HttpStatus.CREATED);

}


// get all wishlist item for a user

@GetMapping("/{token}")
public ResponseEntity<List<ProductDto>> getWishList(@PathVariable("token") String token) {

// authenticate the token
authenticationService.authenticate(token);


// find the user

User user = authenticationService.getUser(token);

List<ProductDto> productDtos = wishListService.getWishListForUser(user);

return new ResponseEntity<>(productDtos, HttpStatus.OK);

}

@Autowired
private WishListService wishListService;

@Autowired
private AuthenticationService authenticationService;

@GetMapping("/{token}")
public ResponseEntity<List<ProductDto>> getWishList(@PathVariable("token") String token) {
int user_id = authenticationService.getUser(token).getId();
List<WishList> body = wishListService.readWishList(user_id);
List<ProductDto> products = new ArrayList<ProductDto>();
for (WishList wishList : body) {
products.add(ProductService.getDtoFromProduct(wishList.getProduct()));
}

return new ResponseEntity<List<ProductDto>>(products, HttpStatus.OK);
}

@PostMapping("/add")
public ResponseEntity<ApiResponse> addWishList(@RequestBody Product product, @RequestParam("token") String token) {
authenticationService.authenticate(token);
User user = authenticationService.getUser(token);
WishList wishList = new WishList(user, product);
wishListService.createWishlist(wishList);
return new ResponseEntity<ApiResponse>(new ApiResponse(true, "Add to wishlist"), HttpStatus.CREATED);

}


}
15 changes: 1 addition & 14 deletions src/main/java/com/webtutsplus/ecommerce/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ public class User {
@Column(name = "email")
private String email;

@Enumerated(EnumType.STRING)
@Column(name = "role")
private Role role;

@Column(name = "password")
private String password;

Expand Down Expand Up @@ -67,14 +63,6 @@ public void setEmail(String email) {
this.email = email;
}

public Role getRole() {
return role;
}

public void setRole(Role role) {
this.role = role;
}

public String getPassword() {
return password;
}
Expand All @@ -83,11 +71,10 @@ public void setPassword(String password) {
this.password = password;
}

public User(String firstName, String lastName, String email, Role role, String password) {
public User(String firstName, String lastName, String email, String password) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.role = role;
this.password = password;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.webtutsplus.ecommerce.repository;


import com.webtutsplus.ecommerce.model.User;
import com.webtutsplus.ecommerce.model.WishList;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
Expand All @@ -10,7 +11,5 @@
@Repository
public interface WishListRepository extends JpaRepository<WishList, Integer> {

List<WishList> findAllByUserIdOrderByCreatedDateDesc(Integer userId);


List<WishList> findAllByUserOrderByCreatedDateDesc(User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ public static ProductDto getDtoFromProduct(Product product) {
return productDto;
}

public ProductDto getProductDto(Product product) {
ProductDto productDto = new ProductDto();
productDto.setDescription(product.getDescription());
productDto.setImageURL(product.getImageURL());
productDto.setName(product.getName());
productDto.setCategoryId(product.getCategory().getId());
productDto.setPrice(product.getPrice());
productDto.setId(product.getId());
return productDto;
}

public static Product getProductFromDto(ProductDto productDto, Category category) {
Product product = new Product(productDto, category);
return product;
Expand Down
49 changes: 1 addition & 48 deletions src/main/java/com/webtutsplus/ecommerce/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public ResponseDto signUp(SignupDto signupDto) throws CustomException {
}


User user = new User(signupDto.getFirstName(), signupDto.getLastName(), signupDto.getEmail(), Role.user, encryptedPassword );
User user = new User(signupDto.getFirstName(), signupDto.getLastName(), signupDto.getEmail(), encryptedPassword );

User createdUser;
try {
Expand Down Expand Up @@ -111,51 +111,4 @@ String hashPassword(String password) throws NoSuchAlgorithmException {
return myHash;
}

public ResponseDto createUser(String token, UserCreateDto userCreateDto) throws CustomException, AuthenticationFailException {
User creatingUser = authenticationService.getUser(token);
if (!canCrudUser(creatingUser.getRole())) {
// user can't create new user
throw new AuthenticationFailException(MessageStrings.USER_NOT_PERMITTED);
}
String encryptedPassword = userCreateDto.getPassword();
try {
encryptedPassword = hashPassword(userCreateDto.getPassword());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
logger.error("hashing password failed {}", e.getMessage());
}

User user = new User(userCreateDto.getFirstName(), userCreateDto.getLastName(), userCreateDto.getEmail(), userCreateDto.getRole(), encryptedPassword );
User createdUser;
try {
createdUser = userRepository.save(user);
final AuthenticationToken authenticationToken = new AuthenticationToken(createdUser);
authenticationService.saveConfirmationToken(authenticationToken);
return new ResponseDto(ResponseStatus.success.toString(), USER_CREATED);
} catch (Exception e) {
// handle user creation fail error
throw new CustomException(e.getMessage());
}

}

boolean canCrudUser(Role role) {
if (role == Role.admin || role == Role.manager) {
return true;
}
return false;
}

boolean canCrudUser(User userUpdating, Integer userIdBeingUpdated) {
Role role = userUpdating.getRole();
// admin and manager can crud any user
if (role == Role.admin || role == Role.manager) {
return true;
}
// user can update his own record, but not his role
if (role == Role.user && userUpdating.getId() == userIdBeingUpdated) {
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.webtutsplus.ecommerce.service;

import java.util.ArrayList;
import java.util.List;
import javax.transaction.Transactional;

import com.webtutsplus.ecommerce.dto.product.ProductDto;
import com.webtutsplus.ecommerce.model.User;
import com.webtutsplus.ecommerce.model.WishList;
import com.webtutsplus.ecommerce.repository.WishListRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


Expand All @@ -13,6 +18,9 @@ public class WishListService {

private final WishListRepository wishListRepository;

@Autowired
ProductService productService;

public WishListService(WishListRepository wishListRepository) {
this.wishListRepository = wishListRepository;
}
Expand All @@ -21,7 +29,13 @@ public void createWishlist(WishList wishList) {
wishListRepository.save(wishList);
}

public List<WishList> readWishList(Integer userId) {
return wishListRepository.findAllByUserIdOrderByCreatedDateDesc(userId);
public List<ProductDto> getWishListForUser(User user) {
final List<WishList> wishLists = wishListRepository.findAllByUserOrderByCreatedDateDesc(user);
List<ProductDto> productDtos = new ArrayList<>();
for (WishList wishList: wishLists) {
productDtos.add(productService.getProductDto(wishList.getProduct()));
}

return productDtos;
}
}