Commit 8965a5e6 authored by Maxym Borodenko's avatar Maxym Borodenko Committed by Matija Obreza

Extended API

parent 7a34fb0e
......@@ -31,7 +31,10 @@ import org.gringlobal.api.v1.Pagination;
import org.gringlobal.custom.elasticsearch.SearchException;
import org.gringlobal.model.CropTrait;
import org.gringlobal.model.CropTraitAttach;
import org.gringlobal.model.CropTraitCode;
import org.gringlobal.model.CropTraitCodeAttach;
import org.gringlobal.model.CropTraitCodeLang;
import org.gringlobal.model.CropTraitLang;
import org.gringlobal.model.QCropTrait;
import org.gringlobal.service.CropTraitAttachmentService;
import org.gringlobal.service.CropTraitCodeAttachmentService;
......@@ -101,6 +104,48 @@ public class CropTraitController extends ApiBaseController {
return new FilteredPage<>(normalizedFilter, cropTraitService.list(filter, pageable));
}
@PostMapping(value = "/code", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "createCropTraitCodeWithLang", description = "Create CropTraitCode and CropTraitLang", summary = "Crate")
public CropTraitService.TranslatedCropTraitCode create(@RequestBody final CropTraitService.TranslatedCropTraitCode code) {
return cropTraitService.create(code);
}
@PutMapping(value = "/code", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "updateCropTraitCode", description = "Update a record of CropTraitCode", summary = "Update")
public CropTraitCode update(@RequestBody final CropTraitCode entity) {
return cropTraitService.update(entity);
}
@DeleteMapping(value = "/code/{id}", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "deleteCropTraitCode", description = "Delete a record of CropTraitCode", summary = "Delete")
public CropTraitCode removeTraitCode(@PathVariable("id") final long id) {
return cropTraitService.remove(cropTraitService.getTraitCode(id));
}
@PutMapping(value = "/lang", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "updateCropTraitLang", description = "Update a record of CropTraitLang", summary = "Update")
public CropTraitLang update(@RequestBody final CropTraitLang entity) {
return cropTraitService.update(entity);
}
@DeleteMapping(value = "/lang/{id}", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "deleteCropTraitLang", description = "Delete a record of CropTraitLang", summary = "Delete")
public CropTraitLang removeTraitLang(@PathVariable("id") final long id) {
return cropTraitService.remove(cropTraitService.getTraitLang(id));
}
@PutMapping(value = "/code-lang", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "updateCropTraitCodeLang", description = "Update a record of CropTraitCodeLang", summary = "Update")
public CropTraitCodeLang update(@RequestBody final CropTraitCodeLang entity) {
return cropTraitService.update(entity);
}
@DeleteMapping(value = "/code-lang/{id}", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "deleteCropTraitCodeLang", description = "Delete a record of CropTraitCodeLang", summary = "Delete")
public CropTraitCodeLang removeCodeLang(@PathVariable("id") final long id) {
return cropTraitService.remove(cropTraitService.getCodeLang(id));
}
/**
* Remove the entity.
*
......@@ -120,8 +165,8 @@ public class CropTraitController extends ApiBaseController {
* @return the recorded record
*/
@PostMapping(value = "", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(description = "Create a record", summary = "Add")
public CropTrait create(@RequestBody final CropTrait entity) {
@Operation(description = "Create a record of CropTrait and CropTraitLang", summary = "Add")
public CropTraitService.TranslatedCropTrait create(@RequestBody final CropTraitService.TranslatedCropTrait entity) {
return cropTraitService.create(entity);
}
......
......@@ -34,6 +34,7 @@ import javax.validation.constraints.Size;
public abstract class CooperatorOwnedLang<T extends CooperatorOwnedModel> extends CooperatorOwnedModel {
private static final long serialVersionUID = -8074420732594755502L;
@NotNull
@ManyToOne(fetch = FetchType.LAZY, cascade = {})
@JoinColumn
protected T entity;
......
......@@ -42,6 +42,7 @@ public class CropTraitCode extends CooperatorOwnedModel {
@Column(nullable = false, length = 30)
private String code;
@NotNull
@ManyToOne(fetch = FetchType.LAZY, cascade = {})
@JoinColumn(name = "crop_trait_id", nullable = false)
private CropTrait cropTrait;
......
......@@ -19,13 +19,20 @@ import java.io.Serializable;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import org.gringlobal.model.CropTrait;
import org.gringlobal.model.CropTraitCode;
import org.gringlobal.model.CropTraitCodeLang;
import org.gringlobal.model.CropTraitLang;
import org.gringlobal.service.filter.CropTraitFilter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
/**
* The Interface CropTraitService.
*/
......@@ -39,6 +46,45 @@ public interface CropTraitService extends CRUDService<CropTrait> {
*/
CropTraitCode getTraitCode(long id);
/**
* Update the entity
* @param entity the CropTraitCode entity
* @return the updated entity
*/
CropTraitCode update(@NotNull CropTraitCode entity);
/**
* Remove the record.
*
* @param entity record to delete
* @return the removed entity
*/
CropTraitCode remove(@NotNull CropTraitCode entity);
/**
* Fetch CropTraitLang by ID
*
* @param id the id
* @return the entity
*/
CropTraitLang getTraitLang(long id);
/**
* Update the entity
*
* @param entity the CropTraitLang entity
* @return the updated entity
*/
CropTraitLang update(@NotNull CropTraitLang entity);
/**
* Remove the record.
*
* @param entity record to delete
* @return the removed entity
*/
CropTraitLang remove(@NotNull CropTraitLang entity);
/** List translated CropTrait records matching the filter.
*
* @param filter the CropTraitFilter
......@@ -56,15 +102,58 @@ public interface CropTraitService extends CRUDService<CropTrait> {
*/
TranslatedCropTrait getTranslatedTrait(long id);
/**
* Create new record of CropTrait and CropTraitLang in a current locale
*
* @param entity the CropTraitCodeLang entity
* @return saved cropTrait and cropTraitLang
*/
TranslatedCropTrait create(@NotNull @Valid TranslatedCropTrait entity);
/**
* Create new record of CropTraitCode and CropTraitCodeLang in a current locale
*
* @param code the TranslatedCropTraitCode entity
* @return saved traitCode and codeLang
*/
TranslatedCropTraitCode create(@NotNull @Valid TranslatedCropTraitCode code);
/**
* Fetch record of CropTraitCodeLang by ID
*
* @param id the id
* @return found record
*/
CropTraitCodeLang getCodeLang(long id);
/**
* Update the entity
*
* @param entity the CropTraitCodeLang entity
* @return the updated entity
*/
CropTraitCodeLang update(@NotNull CropTraitCodeLang entity);
/**
* Remove the record.
*
* @param entity record to delete
* @return the removed entity
*/
CropTraitCodeLang remove(@NotNull CropTraitCodeLang entity);
class TranslatedCropTrait implements Serializable {
private static final long serialVersionUID = -499661865275678776L;
@JsonUnwrapped
@NotNull
public CropTrait cropTrait;
@NotNull
@Size(min = 1, max = 500)
public String title;
public String description;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
public List<TranslatedCropTraitCode> codes;
public static TranslatedCropTrait from(CropTrait cropTrait, String title, String description, List<TranslatedCropTraitCode> codes) {
......@@ -81,7 +170,10 @@ public interface CropTraitService extends CRUDService<CropTrait> {
private static final long serialVersionUID = 4624140853118936977L;
@JsonUnwrapped
@NotNull
public CropTraitCode cropTraitCode;
@NotNull
@Size(min = 1, max = 500)
public String title;
public String description;
......
......@@ -18,6 +18,7 @@ package org.gringlobal.service.impl;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import com.querydsl.core.BooleanBuilder;
......@@ -28,6 +29,7 @@ import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.PathBuilder;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.gringlobal.api.InvalidApiUsageException;
import org.gringlobal.api.NotFoundElement;
......@@ -47,7 +49,9 @@ import org.gringlobal.model.QCropTraitCodeAttach;
import org.gringlobal.model.QCropTraitCodeLang;
import org.gringlobal.model.QCropTraitLang;
import org.gringlobal.model.SysLang;
import org.gringlobal.persistence.CropTraitCodeLangRepository;
import org.gringlobal.persistence.CropTraitCodeRepository;
import org.gringlobal.persistence.CropTraitLangRepository;
import org.gringlobal.persistence.CropTraitRepository;
import org.gringlobal.service.CropTraitAttachmentService;
import org.gringlobal.service.CropTraitAttachmentService.CropTraitAttachmentRequest;
......@@ -71,6 +75,10 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.Validator;
/**
* The Class CropTraitServiceImpl.
*/
......@@ -85,12 +93,21 @@ public class CropTraitServiceImpl extends CRUDServiceImpl<CropTrait, CropTraitRe
@Autowired
private CropTraitCodeRepository traitCodeRepository;
@Autowired
private CropTraitCodeLangRepository codeLangRepository;
@Autowired
private CropTraitLangRepository cropTraitLangRepository;
@Autowired
private LanguageService languageService;
@Autowired
private JPAQueryFactory jpaQueryFactory;
@Autowired
private Validator validator;
@Component(value = "cropTraitAttachmentSupport")
protected static class CropTraitAttachmentSupport extends BaseAttachmentSupport<CropTrait, CropTraitAttach, CropTraitAttachmentRequest> implements CropTraitAttachmentService {
......@@ -176,6 +193,104 @@ public class CropTraitServiceImpl extends CRUDServiceImpl<CropTrait, CropTraitRe
return TranslatedCropTrait.from(cropTrait, cropTraitTranslation.title, cropTraitTranslation.description, codes);
}
@Override
@Transactional
public TranslatedCropTraitCode create(TranslatedCropTraitCode source) {
CropTrait loadedCodeTrait = get(source.cropTraitCode.getCropTrait().getId());
CropTraitCode cropTraitCode = new CropTraitCode();
cropTraitCode.setCode(source.cropTraitCode.getCode());
cropTraitCode.setCropTrait(loadedCodeTrait);
Set<ConstraintViolation<CropTraitCode>> violations = validator.validate(cropTraitCode);
if (CollectionUtils.isEmpty(violations))
cropTraitCode = traitCodeRepository.save(cropTraitCode);
else throw new ConstraintViolationException("Invalid CropTraitCode.", violations);
CropTraitCodeLang codeLang = new CropTraitCodeLang();
codeLang.setTitle(source.title);
codeLang.setDescription(source.description);
codeLang.setSysLang(languageService.getLanguage(LocaleContextHolder.getLocale()));
codeLang.setEntity(cropTraitCode);
Set<ConstraintViolation<CropTraitCodeLang>> violations2 = validator.validate(codeLang);
if (CollectionUtils.isEmpty(violations2))
codeLang = codeLangRepository.save(codeLang);
else throw new ConstraintViolationException("Invalid CropTraitCodeLang.", violations2);
return TranslatedCropTraitCode.from(cropTraitCode, codeLang.getTitle(), codeLang.getDescription());
}
@Override
@Transactional
public CropTraitCode update(CropTraitCode input) {
final CropTraitCode loaded = getTraitCode(input.getId());
LOG.debug("Update CropTraitCode. Input data {}", input);
loaded.setCode(input.getCode());
CropTraitCode saved = traitCodeRepository.save(loaded);
saved.lazyLoad();
return saved;
}
@Override
public CropTraitCodeLang getCodeLang(long id) {
return codeLangRepository.findById(id).orElseThrow(() -> new NotFoundElement("No such record with id=" + id));
}
@Override
@Transactional
public CropTraitCodeLang update(CropTraitCodeLang input) {
final CropTraitCodeLang loaded = getCodeLang(input.getId());
LOG.debug("Update CropTraitCodeLang. Input data {}", input);
loaded.setTitle(input.getTitle());
loaded.setDescription(input.getDescription());
CropTraitCodeLang saved = codeLangRepository.save(loaded);
saved.lazyLoad();
return saved;
}
@Override
@Transactional
public CropTraitCode remove(CropTraitCode entity) {
entity = getTraitCode(entity.getId());
traitCodeRepository.delete(entity);
return entity;
}
@Override
public CropTraitLang getTraitLang(long id) {
return cropTraitLangRepository.findById(id).orElseThrow(() -> new NotFoundElement("No such record with id=" + id));
}
@Override
@Transactional
public CropTraitLang update(CropTraitLang input) {
final CropTraitLang loaded = getTraitLang(input.getId());
LOG.debug("Update CropTraitLang. Input data {}", input);
loaded.setTitle(input.getTitle());
loaded.setDescription(input.getDescription());
CropTraitLang saved = cropTraitLangRepository.save(loaded);
saved.lazyLoad();
return saved;
}
@Override
@Transactional
public CropTraitLang remove(CropTraitLang entity) {
entity = getTraitLang(entity.getId());
cropTraitLangRepository.delete(entity);
return entity;
}
@Override
@Transactional
public CropTraitCodeLang remove(CropTraitCodeLang entity) {
entity = getCodeLang(entity.getId());
codeLangRepository.delete(entity);
return entity;
}
@Override
@Transactional
public CropTrait create(CropTrait source) {
......@@ -189,6 +304,24 @@ public class CropTraitServiceImpl extends CRUDServiceImpl<CropTrait, CropTraitRe
return saved;
}
@Override
@Transactional
public TranslatedCropTrait create(TranslatedCropTrait source) {
CropTrait saved = create(source.cropTrait);
CropTraitLang cropTraitLang = new CropTraitLang();
cropTraitLang.setEntity(saved);
cropTraitLang.setSysLang(languageService.getLanguage(LocaleContextHolder.getLocale()));
cropTraitLang.setTitle(source.title);
cropTraitLang.setDescription(source.description);
Set<ConstraintViolation<CropTraitLang>> violations = validator.validate(cropTraitLang);
if (CollectionUtils.isEmpty(violations))
cropTraitLang = cropTraitLangRepository.save(cropTraitLang);
else throw new ConstraintViolationException("Invalid CropTraitLang.", violations);
return TranslatedCropTrait.from(saved, cropTraitLang.getTitle(), cropTraitLang.getDescription(), null);
}
@Override
@Transactional
public CropTrait update(CropTrait input) {
......
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