Compare commits
4 commits
079a66253a
...
b088ebe821
Author | SHA1 | Date | |
---|---|---|---|
b088ebe821 | |||
a8a7fb1c5a | |||
b0f94bcb4b | |||
|
1dc4c7c65a |
4 changed files with 78 additions and 6 deletions
|
@ -1,30 +1,36 @@
|
||||||
package de.hmmh.pmt;
|
package de.hmmh.pmt;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import de.hmmh.pmt.employee.ApiClientFactory;
|
|
||||||
import de.hmmh.pmt.db.Project;
|
import de.hmmh.pmt.db.Project;
|
||||||
import de.hmmh.pmt.db.ProjectRepository;
|
import de.hmmh.pmt.db.ProjectRepository;
|
||||||
import de.hmmh.pmt.oas.DefaultApi;
|
import de.hmmh.pmt.dtos.CreateProjectDTO;
|
||||||
|
import de.hmmh.pmt.dtos.CreatedProjectDTO;
|
||||||
import de.hmmh.pmt.dtos.GetAllProjectsDTO;
|
import de.hmmh.pmt.dtos.GetAllProjectsDTO;
|
||||||
import de.hmmh.pmt.dtos.ProjectInfo;
|
import de.hmmh.pmt.dtos.ProjectInfo;
|
||||||
|
import de.hmmh.pmt.employee.ApiClientFactory;
|
||||||
|
import de.hmmh.pmt.oas.DefaultApi;
|
||||||
|
import de.hmmh.pmt.util.Mapper;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.client.HttpClientErrorException;
|
||||||
|
import org.springframework.web.client.RestClientException;
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
@RequestMapping("${openapi.projectManagement.base-path:/api/v1}")
|
@RequestMapping("${openapi.projectManagement.base-path:/api/v1}")
|
||||||
public class ApiController implements DefaultApi {
|
public class ApiController implements DefaultApi {
|
||||||
|
@Autowired
|
||||||
|
private Mapper mapper;
|
||||||
@Autowired
|
@Autowired
|
||||||
private ApiClientFactory apiClientFactory;
|
private ApiClientFactory apiClientFactory;
|
||||||
@Autowired
|
@Autowired
|
||||||
private ProjectRepository projectRepository;
|
private ProjectRepository projectRepository;
|
||||||
|
|
||||||
// apiClientFactory.getEmployeeApi().findAll1()
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Optional<ObjectMapper> getObjectMapper() {
|
public Optional<ObjectMapper> getObjectMapper() {
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
@ -48,4 +54,32 @@ public class ApiController implements DefaultApi {
|
||||||
|
|
||||||
return ResponseEntity.ok(response);
|
return ResponseEntity.ok(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ResponseEntity<CreatedProjectDTO> createProject(CreateProjectDTO body) {
|
||||||
|
if (projectRepository.existsByName(body.getName())) {
|
||||||
|
return new ResponseEntity<>(HttpStatus.CONFLICT);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
apiClientFactory.getEmployeeApi().findById(body.getAdministratorId());
|
||||||
|
} catch (HttpClientErrorException exception) {
|
||||||
|
return new ResponseEntity<>(
|
||||||
|
exception.getStatusCode().equals(HttpStatus.NOT_FOUND)
|
||||||
|
? HttpStatus.NOT_FOUND
|
||||||
|
: HttpStatus.SERVICE_UNAVAILABLE
|
||||||
|
);
|
||||||
|
} catch (RestClientException exception) {
|
||||||
|
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
Project project = mapper.map(body);
|
||||||
|
if (!project.isValid()) {
|
||||||
|
return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
|
||||||
|
}
|
||||||
|
projectRepository.save(project);
|
||||||
|
|
||||||
|
CreatedProjectDTO response = mapper.map(project);
|
||||||
|
return new ResponseEntity<>(response, HttpStatus.CREATED);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,5 +43,10 @@ public class Project {
|
||||||
private LocalDateTime plannedEnd;
|
private LocalDateTime plannedEnd;
|
||||||
|
|
||||||
private LocalDateTime realEnd; // Cant be named just "end" because it's and SQL Keyword
|
private LocalDateTime realEnd; // Cant be named just "end" because it's and SQL Keyword
|
||||||
|
|
||||||
|
|
||||||
|
public boolean isValid() {
|
||||||
|
return plannedEnd.isAfter(start) && (realEnd == null || realEnd.isAfter(start));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,4 +3,5 @@ package de.hmmh.pmt.db;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
public interface ProjectRepository extends JpaRepository<Project, Long> {
|
public interface ProjectRepository extends JpaRepository<Project, Long> {
|
||||||
|
boolean existsByName(String name);
|
||||||
}
|
}
|
||||||
|
|
32
src/main/java/de/hmmh/pmt/util/Mapper.java
Normal file
32
src/main/java/de/hmmh/pmt/util/Mapper.java
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
package de.hmmh.pmt.util;
|
||||||
|
|
||||||
|
import de.hmmh.pmt.db.Project;
|
||||||
|
import de.hmmh.pmt.dtos.CreateProjectDTO;
|
||||||
|
import de.hmmh.pmt.dtos.CreatedProjectDTO;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class Mapper {
|
||||||
|
public Project map(CreateProjectDTO dto) {
|
||||||
|
Project project = new Project();
|
||||||
|
project.setName(dto.getName());
|
||||||
|
project.setGoal(dto.getGoal());
|
||||||
|
project.setCustomerId(dto.getCustomerId());
|
||||||
|
project.setAdministratorId(dto.getAdministratorId());
|
||||||
|
project.setStart(dto.getStart());
|
||||||
|
project.setPlannedEnd(dto.getPlannedEnd());
|
||||||
|
return project;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CreatedProjectDTO map(Project project) {
|
||||||
|
CreatedProjectDTO dto = new CreatedProjectDTO();
|
||||||
|
dto.setId(project.getId());
|
||||||
|
dto.setName(project.getName());
|
||||||
|
dto.setGoal(project.getGoal());
|
||||||
|
dto.setCustomerId(project.getCustomerId());
|
||||||
|
dto.setAdministratorId(project.getAdministratorId());
|
||||||
|
dto.setStart(project.getStart());
|
||||||
|
dto.setPlannedEnd(project.getPlannedEnd());
|
||||||
|
return dto;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue