Compare commits

..

1 commit

Author SHA1 Message Date
Rajbir Singh
d84647f9e6 .
Some checks failed
Quality Check / Validate OAS (push) Successful in 34s
Quality Check / Linting (push) Failing after 52s
Quality Check / Static Analysis (push) Failing after 1m0s
Quality Check / Testing (push) Successful in 1m4s
2024-10-07 14:27:22 +02:00
5 changed files with 35 additions and 104 deletions

View file

@ -30,16 +30,16 @@ components:
CreateProjectDTO:
type: object
properties:
name:
type: string
goal:
type: string
customerId:
type: integer
format: int64
administratorId:
type: integer
format: int64
title:
type: string
goal:
type: string
start:
type: string
format: date-time
@ -52,16 +52,16 @@ components:
id:
type: integer
format: int64
name:
type: string
goal:
type: string
customerId:
type: integer
format: int64
administratorId:
type: integer
format: int64
title:
type: string
goal:
type: string
start:
type: string
format: date-time
@ -69,8 +69,14 @@ components:
type: string
format: date-time
responses:
Unauthorized:
description: "Unauthorized"
UnAuthorized:
description: "Un Authorized"
InternalError:
description: "Internal Server Error"
content:
text/plain:
schema:
type: string
NotFound:
description: "Not Found"
content:
@ -83,24 +89,6 @@ components:
text/plain:
schema:
type: string
UnprocessableContent:
description: "Unprocessable Content"
content:
text/plain:
schema:
type: string
InternalError:
description: "Internal Server Error"
content:
text/plain:
schema:
type: string
ServiceUnavailable:
description: "Service Unavailable"
content:
text/plain:
schema:
type: string
paths:
/project:
get:
@ -114,7 +102,7 @@ paths:
schema:
$ref: "#/components/schemas/GetAllProjectsDTO"
401:
$ref: "#/components/responses/Unauthorized"
$ref: "#/components/responses/UnAuthorized"
500:
$ref: "#/components/responses/InternalError"
post:
@ -133,14 +121,10 @@ paths:
schema:
$ref: "#/components/schemas/CreatedProjectDTO"
401:
$ref: "#/components/responses/Unauthorized"
$ref: "#/components/responses/UnAuthorized"
404:
$ref: "#/components/responses/NotFound"
409:
$ref: "#/components/responses/Conflict"
422:
$ref: "#/components/responses/UnprocessableContent"
$ref: "#/components/responses/NotFound"
500:
$ref: "#/components/responses/InternalError"
503:
$ref: "#/components/responses/ServiceUnavailable"
$ref: "#/components/responses/InternalError"

View file

@ -4,7 +4,7 @@
"invokerPackage": "de.hmmh.pmt",
"java8": false,
"java11": true,
"dateLibrary": "java8-localdatetime",
"dateLibrary": "java11",
"library": "spring-boot3",
"defaultInterfaces": false,
"serializableModel": true

View file

@ -1,22 +1,20 @@
package de.hmmh.pmt;
import com.fasterxml.jackson.databind.ObjectMapper;
import de.hmmh.pmt.db.Project;
import de.hmmh.pmt.db.ProjectRepository;
import de.hmmh.pmt.dtos.CreateProjectDTO;
import de.hmmh.pmt.dtos.CreatedProjectDTO;
import de.hmmh.pmt.employee.ApiClientFactory;
import de.hmmh.pmt.db.Project;
import de.hmmh.pmt.db.ProjectRepository;
import de.hmmh.pmt.oas.DefaultApi;
import de.hmmh.pmt.dtos.GetAllProjectsDTO;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestClientException;
import java.util.Optional;
@ -24,8 +22,6 @@ import java.util.Optional;
@Controller
@RequestMapping("${openapi.projectManagement.base-path:/api/v1}")
public class ApiController implements DefaultApi {
@Autowired
private Mapper mapper;
@Autowired
private ApiClientFactory apiClientFactory;
@Autowired
@ -45,7 +41,7 @@ public class ApiController implements DefaultApi {
public ResponseEntity<GetAllProjectsDTO> getAllProjects() {
GetAllProjectsDTO response = new GetAllProjectsDTO();
for (Project project : this.projectRepository.findAll()) {
for (Project project : this.projectRepository.findAll()){
ProjectInfo projectInfo = new ProjectInfo();
projectInfo.setId(project.getId());
projectInfo.setName(project.getName());
@ -57,29 +53,17 @@ public class ApiController implements DefaultApi {
@Override
public ResponseEntity<CreatedProjectDTO> createProject(CreateProjectDTO body) {
if (projectRepository.existsByName(body.getName())) {
if (projectRepository.existsByName(body.getTitle())){
return new ResponseEntity<>(HttpStatus.CONFLICT);
}
try {
if (apiClientFactory.getEmployeeApi().findById(body.getAdministratorId()).getId() == body.getAdministratorId()){
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);
}
}
} 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);
return null;
}
}

View file

@ -19,7 +19,7 @@ import java.time.LocalDateTime;
@Table(name = "project")
public class Project {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank
@ -43,10 +43,5 @@ public class Project {
private LocalDateTime plannedEnd;
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));
}
}

View file

@ -1,32 +0,0 @@
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;
}
}