Commit 71694bce authored by Khalid Ali's avatar Khalid Ali
Browse files

PUT request returns status OK or NOT_FOUND

parent 17cf929d
......@@ -36,8 +36,12 @@ public class ListingService {
return listingRepository.findByIsbn(isbn).get(0);
}
public void updateListing(ListingEntity listingEntity) {
public ListingEntity updateListing(ListingEntity listingEntity) {
if (listingRepository.findById(listingEntity.getId()).isPresent())
listingRepository.save(listingEntity);
else
return null;
return listingEntity;
}
public void deleteListing(Long id) {
......
......@@ -9,6 +9,7 @@ import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.text.ParseException;
......@@ -54,11 +55,15 @@ public class BookshareApiController {
return convertToDto(listingService.getById(id));
}
@PutMapping(value = "/listing/{id}")
@ResponseStatus(HttpStatus.OK)
void updateListing(@RequestBody ListingDto listingDto) throws ParseException {
@PutMapping(value = "/listing/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
ResponseEntity<ListingDto> updateListing(@RequestBody ListingDto listingDto) throws ParseException {
ListingEntity listingEntity = convertToEntity(listingDto);
listingService.updateListing(listingEntity);
ListingEntity ret = listingService.updateListing(listingEntity);
if (ret == null) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
} else {
return new ResponseEntity<>(convertToDto(ret), HttpStatus.OK);
}
}
@DeleteMapping(value = "/listing/{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