Commit 9b0f5678 authored by Khalid Ali's avatar Khalid Ali
Browse files

Add getById and PUT request to controller

parent 6b2f7397
...@@ -14,7 +14,7 @@ public class ListingService { ...@@ -14,7 +14,7 @@ public class ListingService {
private final ListingRepository listingRepository; private final ListingRepository listingRepository;
@Autowired @Autowired
public ListingService(ListingRepository listingRepository) { ListingService(ListingRepository listingRepository) {
this.listingRepository = listingRepository; this.listingRepository = listingRepository;
} }
...@@ -32,7 +32,11 @@ public class ListingService { ...@@ -32,7 +32,11 @@ public class ListingService {
.orElseThrow(() -> new ListingNotFoundException(id)); .orElseThrow(() -> new ListingNotFoundException(id));
} }
public ListingEntity getIsbn(int isbn) { ListingEntity getIsbn(int isbn) {
return listingRepository.findByIsbn(isbn).get(0); return listingRepository.findByIsbn(isbn).get(0);
} }
public void updateListing(ListingEntity listingEntity) {
listingRepository.save(listingEntity);
}
} }
...@@ -11,7 +11,6 @@ import org.springframework.http.HttpStatus; ...@@ -11,7 +11,6 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.text.ParseException;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -43,7 +42,7 @@ public class BookshareApiController { ...@@ -43,7 +42,7 @@ 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) throws ParseException { public ListingDto newListing(@RequestBody ListingDto listingDto) {
ListingEntity post = convertToEntity(listingDto); ListingEntity post = convertToEntity(listingDto);
ListingEntity postCreated = listingService.addListing(post); ListingEntity postCreated = listingService.addListing(post);
return convertToDto(postCreated); return convertToDto(postCreated);
...@@ -54,15 +53,21 @@ public class BookshareApiController { ...@@ -54,15 +53,21 @@ public class BookshareApiController {
return convertToDto(listingService.getById(id)); return convertToDto(listingService.getById(id));
} }
@PutMapping(value = "/listing/{id}")
@ResponseStatus(HttpStatus.OK)
void updateListing(@RequestBody ListingDto listingDto) {
ListingEntity listingEntity = convertToEntity(listingDto);
listingService.updateListing(listingEntity);
}
private ListingDto convertToDto(ListingEntity listingEntity) { private ListingDto convertToDto(ListingEntity listingEntity) {
ListingDto listingDto = modelMapper.map(listingEntity, ListingDto.class); ListingDto listingDto = modelMapper.map(listingEntity, ListingDto.class);
listingDto.setCreateDate(listingEntity.getCreateDate()); listingDto.setCreateDate(listingEntity.getCreateDate());
return listingDto; return listingDto;
} }
private ListingEntity convertToEntity(ListingDto listingDto) throws ParseException { private ListingEntity convertToEntity(ListingDto listingDto) {
ListingEntity post = modelMapper.map(listingDto, ListingEntity.class); // post.setCreateDate(ListingDto.getCreateDateConverted(
// post.setCreateDate(ListingDto.getCreateDateConverted(
// userService.getCurrentUser().getPreference().getTimezone())); // userService.getCurrentUser().getPreference().getTimezone()));
// //
// if (ListingDto.getId() != null) { // if (ListingDto.getId() != null) {
...@@ -70,6 +75,6 @@ public class BookshareApiController { ...@@ -70,6 +75,6 @@ public class BookshareApiController {
// post.setRedditID(oldPost.getRedditID()); // post.setRedditID(oldPost.getRedditID());
// post.setSent(oldPost.isSent()); // post.setSent(oldPost.isSent());
// } // }
return post; return modelMapper.map(listingDto, ListingEntity.class);
} }
} }
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