Search in sources :

Example 6 with Author

use of com.tvd12.ezydata.example.mongo.entity.Author in project ezyfox-examples by tvd12.

the class BookController method addBook.

@DoPost("/book/add")
public BookResponse addBook(@RequestBody AddBookRequest request) {
    BookNameAndAuthorId bookNameAndAuthorId = new BookNameAndAuthorId(request.getBookName(), request.getAuthorId());
    Long existedBookId = bookIdByNameAndAuthorIdMap.get(bookNameAndAuthorId);
    if (existedBookId != null) {
        throw new HttpBadRequestException("author: " + request.getAuthorId() + " has already registered book: " + request.getBookName());
    }
    Author author = authorMap.get(request.getAuthorId());
    if (author == null) {
        throw new HttpBadRequestException("author: " + request.getAuthorId() + " not found");
    }
    Category category = categoryMap.get(request.getCategoryId());
    if (category == null) {
        throw new HttpBadRequestException("category: " + request.getCategoryId() + " not found");
    }
    val bookId = idGentor.incrementAndGet();
    val book = requestToEntityConverter.toBookEntity(request, bookId);
    bookMap.put(book.getId(), book);
    bookIdByNameAndAuthorIdMap.put(bookNameAndAuthorId, bookId);
    return entityToResponseConverter.toBookResponse(book, author, category);
}
Also used : lombok.val(lombok.val) Category(com.tvd12.ezydata.example.redis.entity.Category) BookNameAndAuthorId(com.tvd12.ezydata.example.redis.entity.BookNameAndAuthorId) EzyRedisAtomicLong(com.tvd12.ezydata.redis.EzyRedisAtomicLong) HttpBadRequestException(com.tvd12.ezyhttp.core.exception.HttpBadRequestException) Author(com.tvd12.ezydata.example.redis.entity.Author)

Example 7 with Author

use of com.tvd12.ezydata.example.mongo.entity.Author in project ezyfox-examples by tvd12.

the class BookController method getBook.

@DoGet("/books/{bookId}")
public BookResponse getBook(@PathVariable Long bookId) {
    Book book = bookMap.get(bookId);
    if (book == null) {
        throw new HttpNotFoundException("not found book with id: " + bookId);
    }
    Author author = authorMap.get(book.getAuthorId());
    Category category = categoryMap.get(book.getCategoryId());
    return entityToResponseConverter.toBookResponse(book, author, category);
}
Also used : HttpNotFoundException(com.tvd12.ezyhttp.core.exception.HttpNotFoundException) Category(com.tvd12.ezydata.example.redis.entity.Category) Book(com.tvd12.ezydata.example.redis.entity.Book) Author(com.tvd12.ezydata.example.redis.entity.Author)

Example 8 with Author

use of com.tvd12.ezydata.example.mongo.entity.Author in project ezyfox-examples by tvd12.

the class DataToEntityConverter method toEntity.

public Author toEntity(AddAuthorData data) {
    final Author entity = new Author();
    entity.setName(data.getAuthorName());
    entity.setCreatedTime(LocalDateTime.now());
    entity.setUpdatedTime(LocalDateTime.now());
    return entity;
}
Also used : Author(com.tvd12.ezydata.example.jpa.entity.Author)

Example 9 with Author

use of com.tvd12.ezydata.example.mongo.entity.Author in project java-examples by tvd12.

the class BookController method getBook.

@GetMapping("/books/{bookId}")
public BookResponse getBook(@PathVariable Long bookId) {
    Book book = bookRepository.findById(bookId).orElseThrow(() -> new HttpNotFoundException("not found book with id: " + bookId));
    Author author = authorRepository.findById(book.getAuthorId()).orElseThrow(() -> new IllegalStateException("maybe someone has change redis"));
    Category category = categoryRepository.findById(book.getCategoryId()).orElseThrow(() -> new IllegalStateException("maybe someone has change redis"));
    return entityToResponseConverter.toBookResponse(book, author, category);
}
Also used : HttpNotFoundException(com.tvd12.example.spring_boot_redis.exception.HttpNotFoundException) Category(com.tvd12.example.spring_boot_redis.entity.Category) Book(com.tvd12.example.spring_boot_redis.entity.Book) Author(com.tvd12.example.spring_boot_redis.entity.Author)

Example 10 with Author

use of com.tvd12.ezydata.example.mongo.entity.Author in project ezyfox-examples by tvd12.

the class AuthorService method saveAuthor.

public AuthorData saveAuthor(AddAuthorData data) {
    final Author entity = dataToEntityConverter.toEntity(data);
    authorRepository.save(entity);
    return entityToDataConverter.toData(entity);
}
Also used : Author(com.tvd12.ezydata.example.jpa.entity.Author)

Aggregations

Author (com.tvd12.ezydata.example.jpa.entity.Author)5 Author (com.tvd12.example.spring_boot_redis.entity.Author)3 Book (com.tvd12.ezydata.example.jpa.entity.Book)3 Category (com.tvd12.ezydata.example.jpa.entity.Category)3 Author (com.tvd12.ezydata.example.mongo.entity.Author)3 Author (com.tvd12.ezydata.example.redis.entity.Author)3 Book (com.tvd12.example.spring_boot_redis.entity.Book)2 Category (com.tvd12.example.spring_boot_redis.entity.Category)2 Book (com.tvd12.ezydata.example.mongo.entity.Book)2 Category (com.tvd12.ezydata.example.mongo.entity.Category)2 Category (com.tvd12.ezydata.example.redis.entity.Category)2 HttpBadRequestException (com.tvd12.ezyhttp.core.exception.HttpBadRequestException)2 HttpNotFoundException (com.tvd12.ezyhttp.core.exception.HttpNotFoundException)2 DoPost (com.tvd12.ezyhttp.server.core.annotation.DoPost)2 lombok.val (lombok.val)2 BookNameAndAuthorId (com.tvd12.example.spring_boot_redis.entity.BookNameAndAuthorId)1 HttpBadRequestException (com.tvd12.example.spring_boot_redis.exception.HttpBadRequestException)1 HttpNotFoundException (com.tvd12.example.spring_boot_redis.exception.HttpNotFoundException)1 AddBookData (com.tvd12.ezydata.example.jpa.data.AddBookData)1 BookData (com.tvd12.ezydata.example.jpa.data.BookData)1