use of com.tvd12.ezyhttp.server.core.annotation.RequestBody in project java-examples by tvd12.
the class AuthorController method addAuthor.
@PostMapping("/add")
public Author addAuthor(@RequestBody AddAuthorRequest request) {
Author author = new Author(idGentor.incrementAndGet("author"), request.getAuthorName());
authorRepository.save(author);
return author;
}
use of com.tvd12.ezyhttp.server.core.annotation.RequestBody in project java-examples by tvd12.
the class BookController method addBook.
@PostMapping("/book/add")
public BookResponse addBook(@RequestBody AddBookRequest request) {
BookNameAndAuthorId bookNameAndAuthorId = new BookNameAndAuthorId(request.getBookName(), request.getAuthorId());
Long existedBookId = bookIdByNameAndAuthorIdRepository.findById(bookNameAndAuthorId).orElse(null);
if (existedBookId != null) {
throw new HttpBadRequestException("author: " + request.getAuthorId() + " has already registered book: " + request.getBookName());
}
Author author = authorRepository.findById(request.getAuthorId()).orElseThrow(() -> new HttpBadRequestException("author: " + request.getAuthorId() + " not found"));
Category category = categoryRepository.findById(request.getCategoryId()).orElseThrow(() -> new HttpBadRequestException("category: " + request.getCategoryId() + " not found"));
Long bookId = idGentor.incrementAndGet("book");
Book book = requestToEntityConverter.toBookEntity(request, bookId);
bookRepository.save(book);
bookIdByNameAndAuthorIdRepository.put(bookNameAndAuthorId, bookId);
return entityToResponseConverter.toBookResponse(book, author, category);
}
use of com.tvd12.ezyhttp.server.core.annotation.RequestBody in project java-examples by tvd12.
the class CategoryController method addCategory.
@PostMapping("/add")
public Category addCategory(@RequestBody AddCategoryRequest request) {
Long existedCategoryId = categoryIdByNameRepository.findById(request.getCategoryName()).orElse(null);
if (existedCategoryId != null) {
throw new HttpBadRequestException("category named: " + request.getCategoryName() + " existed");
}
Category category = new Category(idGentor.incrementAndGet("category"), request.getCategoryName());
categoryRepository.save(category);
categoryIdByNameRepository.put(category.getName(), category.getId());
return category;
}
use of com.tvd12.ezyhttp.server.core.annotation.RequestBody in project ezyhttp by youngmonkeys.
the class RequestHandlerImplementer method makeHandleRequestMethodContent.
@SuppressWarnings("MethodLength")
protected String makeHandleRequestMethodContent() {
EzyMethod method = getHandleRequestMethod();
EzyFunction function = new EzyFunction(method).throwsException();
EzyBody body = function.body();
int paramCount = 0;
int headerCount = 0;
int parameterCount = 0;
int pathVariableCount = 0;
int cookieCount = 0;
Parameter[] parameters = handlerMethod.getParameters();
for (Parameter parameter : parameters) {
Class<?> parameterType = parameter.getType();
Class<?> genericType = getGenericType(parameter);
String genericTypeClass = genericType != null ? genericType.getName() + ".class" : "null";
EzyInstruction instruction = new EzyInstruction("\t", "\n").clazz(parameterType).append(" ").append(PARAMETER_PREFIX).append(paramCount).equal();
boolean hasAnnotation = false;
RequestParam requestParamAnno = parameter.getAnnotation(RequestParam.class);
if (requestParamAnno != null) {
String paramKey = RequestParamAnnotations.getParamKeyString(requestParamAnno, parameterCount);
String defaultValue = requestParamAnno.defaultValue();
String getValueExpression = defaultValue.equals(EzyStrings.NULL) ? "arg0.getParameter(" + paramKey + ")" : "arg0.getParameter(" + paramKey + ", " + quote(defaultValue) + ")";
String valueExpression = "this.deserializeParameter(" + paramKey + ", " + getValueExpression + ", " + parameterType.getTypeName() + ".class" + ", " + genericTypeClass + ")";
instruction.cast(parameterType, valueExpression);
++parameterCount;
hasAnnotation = true;
}
RequestHeader requestHeaderAnno = parameter.getAnnotation(RequestHeader.class);
if (requestHeaderAnno != null) {
String headerKey = RequestHeaderAnnotations.getHeaderKeyString(requestHeaderAnno, headerCount);
String defaultValue = requestHeaderAnno.defaultValue();
String getValueExpression = defaultValue.equals(EzyStrings.NULL) ? "arg0.getHeader(" + headerKey + ")" : "arg0.getHeader(" + headerKey + ", " + quote(defaultValue) + ")";
String valueExpression = "this.deserializeHeader(" + headerKey + ", " + getValueExpression + ", " + parameterType.getTypeName() + ".class" + ", " + genericTypeClass + ")";
instruction.cast(parameterType, valueExpression);
++headerCount;
hasAnnotation = true;
}
PathVariable pathVariableAnno = parameter.getAnnotation(PathVariable.class);
if (pathVariableAnno != null) {
String varNameKey = PathVariableAnnotations.getVariableNameKeyString(pathVariableAnno, pathVariableCount);
String valueExpression = "this.deserializePathVariable(" + varNameKey + ", arg0.getPathVariable(" + varNameKey + ")" + ", " + parameterType.getTypeName() + ".class" + ", " + genericTypeClass + ")";
instruction.cast(parameterType, valueExpression);
++pathVariableCount;
hasAnnotation = true;
}
RequestCookie requestCookieAnno = parameter.getAnnotation(RequestCookie.class);
if (requestCookieAnno != null) {
String cookieKey = RequestCookieAnnotations.getCookieKeyString(requestCookieAnno, cookieCount);
String defaultValue = requestCookieAnno.defaultValue();
String getValueExpression = defaultValue.equals(EzyStrings.NULL) ? "arg0.getCookieValue(" + cookieKey + ")" : "arg0.getCookieValue(" + cookieKey + ", " + quote(defaultValue) + ")";
String valueExpression = "this.deserializeCookie(" + cookieKey + ", " + getValueExpression + ", " + parameterType.getTypeName() + ".class" + ", " + genericTypeClass + ")";
instruction.cast(parameterType, valueExpression);
++cookieCount;
hasAnnotation = true;
}
RequestBody requestBodyAnno = parameter.getAnnotation(RequestBody.class);
if (requestBodyAnno != null) {
instruction.brackets(parameterType).append("this.deserializeBody(").append("arg0, ").clazz(parameterType, true).append(")");
hasAnnotation = true;
}
if (!hasAnnotation) {
String valueExpression = "arg0";
if (parameterType != RequestArguments.class) {
String argumentKey = RequestParameters.getArgumentKeyString(parameter);
valueExpression = "arg0.getArgument(" + argumentKey + ")";
}
instruction.cast(parameterType, valueExpression);
}
body.append(instruction);
++paramCount;
}
EzyInstruction instruction = new EzyInstruction("\t", "\n");
Class<?> returnType = handlerMethod.getReturnType();
if (returnType != void.class) {
instruction.answer();
}
StringBuilder answerExpression = new StringBuilder();
answerExpression.append("this.controller.").append(handlerMethod.getName()).append("(");
for (int i = 0; i < paramCount; ++i) {
answerExpression.append(PARAMETER_PREFIX).append(i);
if (i < paramCount - 1) {
answerExpression.append(", ");
}
}
answerExpression.append(")");
if (returnType != void.class) {
instruction.valueOf(returnType, answerExpression.toString());
} else {
instruction.append(answerExpression);
}
body.append(instruction);
if (returnType == void.class) {
body.append(new EzyInstruction("\t", "\n").append("return null"));
}
return function.toString();
}
use of com.tvd12.ezyhttp.server.core.annotation.RequestBody 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);
}
Aggregations