Commit 5f57a821 authored by Khalid Ali's avatar Khalid Ali
Browse files

Feature/6-Add-Listing-To-User

parent e823ac56
...@@ -3,8 +3,12 @@ package com.gmu.bookshare.service; ...@@ -3,8 +3,12 @@ package com.gmu.bookshare.service;
import com.gmu.bookshare.entity.ShareUser; import com.gmu.bookshare.entity.ShareUser;
import com.gmu.bookshare.persistence.ShareUserRepository; import com.gmu.bookshare.persistence.ShareUserRepository;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
...@@ -41,4 +45,22 @@ public class ShareUserService { ...@@ -41,4 +45,22 @@ public class ShareUserService {
Optional<ShareUser> shareUser = shareUserRepository.findById(id); Optional<ShareUser> shareUser = shareUserRepository.findById(id);
return shareUser.orElse(null); return shareUser.orElse(null);
} }
public ShareUser getShareUser() {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth != null
&& auth.getPrincipal() != null
&& auth.getPrincipal() instanceof UserDetails) {
String username = ((UserDetails) auth.getPrincipal()).getUsername();
ShareUser user = getShareUserByEmail(username);
if (user == null) {
ShareUser newUser = new ShareUser(username, "default", new HashSet<>(), new HashSet<>());
user = addShareUser(newUser);
}
return user;
} else {
return new ShareUser();
}
}
} }
...@@ -25,7 +25,6 @@ import org.springframework.web.bind.annotation.*; ...@@ -25,7 +25,6 @@ import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -59,9 +58,13 @@ public class BookshareApiController { ...@@ -59,9 +58,13 @@ public class BookshareApiController {
@PostMapping(value = "/listing", consumes = MediaType.APPLICATION_JSON_VALUE) @PostMapping(value = "/listing", consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.CREATED) @ResponseStatus(HttpStatus.CREATED)
public ListingDto newListing(@RequestBody ListingDto listingDto) { public ListingDto newListing(@RequestBody ListingDto listingDto) {
ListingEntity post = convertToEntity(listingDto);
ListingEntity postCreated = listingService.addListing(post); ShareUser user = shareUserService.getShareUser();
return convertToDto(postCreated); ListingEntity listingEntity = convertToEntity(listingDto);
user.addListing(listingEntity);
ListingEntity listingCreated = listingService.addListing(listingEntity);
return convertToDto(listingCreated);
} }
@GetMapping(value = "/listing/{id}") @GetMapping(value = "/listing/{id}")
...@@ -102,21 +105,10 @@ public class BookshareApiController { ...@@ -102,21 +105,10 @@ public class BookshareApiController {
return convertBidToDto(bidCreated); return convertBidToDto(bidCreated);
} }
@GetMapping(value = "/user/") @GetMapping(value = "/user")
ShareUserDto getUser() { ShareUserDto getUser() {
Authentication auth = SecurityContextHolder.getContext().getAuthentication(); ShareUser user = shareUserService.getShareUser();
if (auth != null return convertShareUserToDto(user);
&& auth.getPrincipal() != null
&& auth.getPrincipal() instanceof UserDetails) {
String username = ((UserDetails) auth.getPrincipal()).getUsername();
ShareUser user = shareUserService.getShareUserByEmail(username);
if (user == null) {
ShareUser newUser = new ShareUser(username, "default", new HashSet<>(), new HashSet<>());
user = shareUserService.addShareUser(newUser);
}
return convertShareUserToDto(user);
}
return new ShareUserDto("", "");
} }
@GetMapping(value = "/user/id/{id}") @GetMapping(value = "/user/id/{id}")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment