Added book controller
This commit is contained in:
parent
7285c88273
commit
c4cb962578
|
@ -3,6 +3,7 @@ package com.fnecan.study.bookapi.api;
|
|||
import com.fnecan.study.bookapi.service.BookService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
|
@ -32,4 +33,15 @@ public class BookController {
|
|||
bookService.getBookDetails(userId, bookId)
|
||||
);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{bookId}")
|
||||
public ResponseEntity removeBook(
|
||||
@RequestHeader("userId") String userId,
|
||||
@PathVariable("bookId") String bookId
|
||||
){
|
||||
return ResponseEntity.ok(
|
||||
bookService.removeBook(userId, bookId)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ import org.springframework.data.mongodb.repository.MongoRepository;
|
|||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface BookRepository extends MongoRepository<BookDocument, String> {
|
||||
public interface BookRepository extends MongoRepository<BookDocument, String>, BookRepositoryCustom {
|
||||
List<BookDocument> findByUserId(String userId);
|
||||
Optional<BookDocument> findByUserIdAndId(String userId, String bookId);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
package com.fnecan.study.bookapi.repository;
|
||||
|
||||
import com.fnecan.study.bookapi.model.BookDocument;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface BookRepositoryCustom {
|
||||
Optional<BookDocument> removeBook(String userId, String bookId);
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.fnecan.study.bookapi.repository;
|
||||
|
||||
import com.fnecan.study.bookapi.model.BookDocument;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.data.mongodb.core.query.Criteria;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class BookRepositoryCustomImplementation implements BookRepositoryCustom {
|
||||
|
||||
@Autowired
|
||||
MongoTemplate mongoTemplate;
|
||||
|
||||
@Override
|
||||
public Optional<BookDocument> removeBook(String userId, String bookId) {
|
||||
return Optional.ofNullable(mongoTemplate.findAndRemove(
|
||||
Query.query(
|
||||
Criteria.where("userId").is(userId).and("id").is(bookId)
|
||||
), BookDocument.class));
|
||||
}
|
||||
}
|
|
@ -21,4 +21,8 @@ public class BookService {
|
|||
public Optional<BookDocument> getBookDetails(String userId, String bookId) {
|
||||
return bookRepository.findByUserIdAndId(userId, bookId);
|
||||
}
|
||||
|
||||
public Optional<BookDocument> removeBook(String userId, String bookId) {
|
||||
return bookRepository.removeBook(userId, bookId);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue