Search in sources :

Example 1 with DoPost

use of com.tvd12.ezyhttp.server.core.annotation.DoPost in project ezyhttp by youngmonkeys.

the class RequestHandlerMethod method fetchHttpMethod.

protected HttpMethod fetchHttpMethod() {
    DoGet doGet = method.getAnnotation(DoGet.class);
    if (doGet != null) {
        return HttpMethod.GET;
    }
    DoPost doPost = method.getAnnotation(DoPost.class);
    if (doPost != null) {
        return HttpMethod.POST;
    }
    DoPut doPut = method.getAnnotation(DoPut.class);
    if (doPut != null) {
        return HttpMethod.PUT;
    }
    return HttpMethod.DELETE;
}
Also used : DoPost(com.tvd12.ezyhttp.server.core.annotation.DoPost) DoPut(com.tvd12.ezyhttp.server.core.annotation.DoPut) DoGet(com.tvd12.ezyhttp.server.core.annotation.DoGet)

Example 2 with DoPost

use of com.tvd12.ezyhttp.server.core.annotation.DoPost in project ezyfox-examples by tvd12.

the class BookController method addBook.

@DoPost("/book/add")
public BookResponse addBook(@RequestBody AddBookRequest request) {
    Book existedBook = bookRepository.findByNameAndAuthorId(request.getBookName(), request.getAuthorId());
    if (existedBook != null) {
        throw new HttpBadRequestException("author: " + request.getAuthorId() + " has already registered book: " + request.getBookName());
    }
    Author author = authorRepository.findById(request.getAuthorId());
    if (author == null) {
        throw new HttpBadRequestException("author: " + request.getAuthorId() + " not found");
    }
    Category category = categoryRepository.findById(request.getCategoryId());
    if (category == null) {
        throw new HttpBadRequestException("category: " + request.getCategoryId() + " not found");
    }
    val bookId = maxIdRepository.incrementAndGet("book");
    val book = requestToEntityConverter.toBookEntity(request, bookId);
    bookRepository.save(book);
    return entityToResponseConverter.toBookResponse(book, author, category);
}
Also used : lombok.val(lombok.val) Category(com.tvd12.ezydata.example.mongo.entity.Category) Book(com.tvd12.ezydata.example.mongo.entity.Book) HttpBadRequestException(com.tvd12.ezyhttp.core.exception.HttpBadRequestException) Author(com.tvd12.ezydata.example.mongo.entity.Author)

Example 3 with DoPost

use of com.tvd12.ezyhttp.server.core.annotation.DoPost in project ezyfox-examples by tvd12.

the class CategoryController method addCategory.

@DoPost("/add")
public Category addCategory(@RequestBody AddCategoryRequest request) {
    Category existedCategory = categoryRepository.findByName(request.getCategoryName());
    if (existedCategory != null) {
        throw new HttpBadRequestException("category named: " + request.getCategoryName() + " existed");
    }
    Category category = new Category(maxIdRepository.incrementAndGet("category"), request.getCategoryName());
    categoryRepository.save(category);
    return category;
}
Also used : Category(com.tvd12.ezydata.example.mongo.entity.Category) HttpBadRequestException(com.tvd12.ezyhttp.core.exception.HttpBadRequestException) DoPost(com.tvd12.ezyhttp.server.core.annotation.DoPost)

Example 4 with DoPost

use of com.tvd12.ezyhttp.server.core.annotation.DoPost 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 5 with DoPost

use of com.tvd12.ezyhttp.server.core.annotation.DoPost in project ezyfox-examples by tvd12.

the class CategoryController method addCategory.

@DoPost("/add")
public Category addCategory(@RequestBody AddCategoryRequest request) {
    Long existedCategoryId = categoryIdByNameMap.get(request.getCategoryName());
    if (existedCategoryId != null) {
        throw new HttpBadRequestException("category named: " + request.getCategoryName() + " existed");
    }
    Category category = new Category(idGentor.incrementAndGet(), request.getCategoryName());
    categoryMap.put(category.getId(), category);
    categoryIdByNameMap.put(category.getName(), category.getId());
    return category;
}
Also used : Category(com.tvd12.ezydata.example.redis.entity.Category) EzyRedisAtomicLong(com.tvd12.ezydata.redis.EzyRedisAtomicLong) HttpBadRequestException(com.tvd12.ezyhttp.core.exception.HttpBadRequestException) DoPost(com.tvd12.ezyhttp.server.core.annotation.DoPost)

Aggregations

DoPost (com.tvd12.ezyhttp.server.core.annotation.DoPost)9 HttpBadRequestException (com.tvd12.ezyhttp.core.exception.HttpBadRequestException)4 DoGet (com.tvd12.ezyhttp.server.core.annotation.DoGet)3 DoPut (com.tvd12.ezyhttp.server.core.annotation.DoPut)3 Author (com.tvd12.ezydata.example.mongo.entity.Author)2 Category (com.tvd12.ezydata.example.mongo.entity.Category)2 Author (com.tvd12.ezydata.example.redis.entity.Author)2 Category (com.tvd12.ezydata.example.redis.entity.Category)2 EzyRedisAtomicLong (com.tvd12.ezydata.redis.EzyRedisAtomicLong)2 DoDelete (com.tvd12.ezyhttp.server.core.annotation.DoDelete)2 lombok.val (lombok.val)2 AddAuthorData (com.tvd12.ezydata.example.jpa.data.AddAuthorData)1 AddBookData (com.tvd12.ezydata.example.jpa.data.AddBookData)1 AddCategoryData (com.tvd12.ezydata.example.jpa.data.AddCategoryData)1 AuthorData (com.tvd12.ezydata.example.jpa.data.AuthorData)1 BookData (com.tvd12.ezydata.example.jpa.data.BookData)1 CategoryData (com.tvd12.ezydata.example.jpa.data.CategoryData)1 Book (com.tvd12.ezydata.example.mongo.entity.Book)1 BookNameAndAuthorId (com.tvd12.ezydata.example.redis.entity.BookNameAndAuthorId)1 User (org.youngmonkeys.example.ezyhttp.login.entity.User)1