Skip to content

Instantly share code, notes, and snippets.

@stephanetimmermans
Last active Aug 20, 2017
Embed
What would you like to do?
IntelliJ Code Template
#set( $name = ${NAME.substring(0,1).toLowerCase()}+${NAME.substring(1)} )
#set( $Names = ${NAME}+"s" )
#set( $names = ${name}+"s" )
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
#parse("File Header.java")
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class ${NAME}DTO {
private Long id;
private String name;
private String description;
}
#set( $name = ${NAME.substring(0,1).toLowerCase()}+${NAME.substring(1)} )
#set( $Names = ${NAME}+"s" )
#set( $names = ${name}+"s" )
import lombok.Data;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.*;
import java.time.LocalDateTime;
#parse("File Header.java")
@Data
@Entity
@EntityListeners(AuditingEntityListener.class)
public class ${NAME} {
@Id
@GeneratedValue
private Long id;
@CreatedDate
private LocalDateTime createdDate;
@LastModifiedDate
private LocalDateTime lastModifiedDate;
private String name;
private String description;
}
#set( $name = ${NAME.substring(0,1).toLowerCase()}+${NAME.substring(1)} )
#set( $Names = ${NAME}+"s" )
#set( $names = ${name}+"s" )
import lombok.RequiredArgsConstructor;
import org.hibernate.envers.AuditReader;
import org.hibernate.envers.AuditReaderFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.ArrayList;
import java.util.List;
#parse("File Header.java")
@Repository
@Transactional
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ${NAME}HistoryRepository {
@PersistenceContext
private EntityManager entityManager;
private final ${NAME}Mapper ${name}Mapper;
public List<${NAME}DTO> getRevisions(Long id) {
AuditReader auditReader = AuditReaderFactory.get(entityManager);
List<Number> revisions = auditReader.getRevisions(${NAME}.class, id);
List<${NAME}DTO> ${names} = new ArrayList<>();
for (Number revision : revisions) {
${NAME} ${name} = auditReader.find(${NAME}.class, id, revision);
${names}.add(${name}Mapper.${name}To${NAME}DTO(${name}));
}
return ${names};
}
}
#set( $name = ${NAME.substring(0,1).toLowerCase()}+${NAME.substring(1)} )
#set( $Names = ${NAME}+"s" )
#set( $names = ${name}+"s" )
import org.mapstruct.Mapper;
#parse("File Header.java")
@Mapper(componentModel = "spring")
public interface ${NAME}Mapper {
${NAME}DTO ${name}To${NAME}DTO(${NAME} ${name});
${NAME} ${name}DTOTo${NAME}(${NAME}DTO ${name}DTO);
}
#set( $name = ${NAME.substring(0,1).toLowerCase()}+${NAME.substring(1)} )
#set( $Names = ${NAME}+"s" )
#set( $names = ${name}+"s" )
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.Optional;
#parse("File Header.java")
public interface ${NAME}Repository extends JpaRepository<${NAME}, Long> {
Optional<${NAME}> findByNameIgnoreCase(String name);
}
#set( $name = ${NAME.substring(0,1).toLowerCase()}+${NAME.substring(1)} )
#set( $Names = ${NAME}+"s" )
#set( $names = ${name}+"s" )
import be.akala.error.ErrorDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Optional;
#parse("File Header.java")
@Slf4j
@RestController
@RequestMapping("/${name}s")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ${NAME}RestController {
private final ${NAME}Service ${name}Service;
private final static String CREATION_ERROR = "Error creating ${name}";
private final static String NOT_FOUND_ERROR = "No ${name} found for id: ";
private final static String UPDATING_ERROR = "Error updating ${name} for id: ";
@PostMapping
ResponseEntity<?> create${NAME}(@RequestBody ${NAME}DTO ${name}DTO) {
Optional<${NAME}DTO> ${name} = ${name}Service.create(${name}DTO);
if (${name}.isPresent()) {
return new ResponseEntity<>(${name}.get(), HttpStatus.CREATED);
}
log.error(CREATION_ERROR);
return new ResponseEntity<>(new ErrorDTO(CREATION_ERROR), HttpStatus.INTERNAL_SERVER_ERROR);
}
@GetMapping
ResponseEntity<?> get${NAME}s(Pageable pageable) {
Page<${NAME}DTO> ${name}s = ${name}Service.retrieve(pageable);
if (${name}s.getContent().isEmpty()) {
return new ResponseEntity<>(new ErrorDTO("No ${name}s found"), HttpStatus.NOT_FOUND);
}
return new ResponseEntity<>(${name}s.getContent(), HttpStatus.OK);
}
@GetMapping("/{id}")
ResponseEntity<?> find${NAME}(@PathVariable Long id) {
Optional<${NAME}DTO> ${name} = ${name}Service.retrieve(id);
if (${name}.isPresent()) {
return new ResponseEntity<>(${name}.get(), HttpStatus.OK);
}
log.error(NOT_FOUND_ERROR + id);
return new ResponseEntity<>(new ErrorDTO(NOT_FOUND_ERROR + id), HttpStatus.NOT_FOUND);
}
@PutMapping("/{id}")
ResponseEntity<?> update${NAME}(@PathVariable Long id, @RequestBody ${NAME}DTO ${name}DTO) {
${name}DTO.setId(id);
Optional<${NAME}DTO> ${name}Optional = ${name}Service.update(${name}DTO);
if (${name}Optional.isPresent()) {
return new ResponseEntity<>(${name}Optional.get(), HttpStatus.OK);
}
log.error(UPDATING_ERROR + id);
return new ResponseEntity<>(new ErrorDTO(UPDATING_ERROR + id), HttpStatus.INTERNAL_SERVER_ERROR);
}
@DeleteMapping("/{id}")
ResponseEntity<?> delete${NAME}(@PathVariable Long id) {
${name}Service.delete(id);
return new ResponseEntity<>(HttpStatus.OK);
}
@ExceptionHandler(Exception.class)
ResponseEntity<?> handleException(Exception ex) {
ErrorDTO errorDTO = new ErrorDTO("Error retrieving ${name}s");
errorDTO.setMessage(ex.getMessage());
return new ResponseEntity<>(errorDTO, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
#set( $name = ${NAME.substring(0,1).toLowerCase()}+${NAME.substring(1)} )
#set( $Names = ${NAME}+"s" )
#set( $names = ${name}+"s" )
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Optional;
#parse("File Header.java")
@Service
@Transactional
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ${NAME}Service {
private final ${NAME}Repository ${name}Repository;
private final ${NAME}Mapper ${name}Mapper;
private final int PAGE_SIZE = 20;
public Optional<${NAME}DTO> create(${NAME}DTO ${name}DTO) {
${NAME} ${name} = ${name}Mapper.${name}DTOTo${NAME}(${name}DTO);
${name}Repository.save(${name});
${name}DTO = ${name}Mapper.${name}To${NAME}DTO(${name});
return Optional.of(${name}DTO);
}
public Page<${NAME}DTO> retrieve(Pageable pageable) {
return ${name}Repository.findAll(pageable).map(${name}Mapper::${name}To${NAME}DTO);
}
public Page<${NAME}DTO> retrieve() {
return retrieve(PageRequest.of(0, PAGE_SIZE));
}
public Optional<${NAME}DTO> retrieve(Long id) {
return ${name}Repository.findById(id).map(${name}Mapper::${name}To${NAME}DTO);
}
public Optional<${NAME}DTO> update(${NAME}DTO ${name}DTO) {
${NAME} ${name} = ${name}Mapper.${name}DTOTo${NAME}(${name}DTO);
${name}Repository.save(${name});
${name}DTO = ${name}Mapper.${name}To${NAME}DTO(${name});
return Optional.of(${name}DTO);
}
public Optional<${NAME}> delete(Long id) {
${NAME} ${name} = ${name}Repository.findById(id).get();
// ${name}.setActive(false);
${name}Repository.save(${name});
return Optional.of(${name});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment