Commit e1bf6a00 authored by Samuel Kulíšek's avatar Samuel Kulíšek
Browse files

Add update mission api

parent 9ecd0aba
Pipeline #142715 passed with stage
in 1 minute and 24 seconds
package cz.muni.fi.pa165.dto;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class AgentNameUpdateDto {
Long id;
String codename;
}
package cz.muni.fi.pa165.dto;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.sql.Date;
import java.util.Set;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class MissionCreateDto {
private String codename;
private String objectives;
private Integer durationDays;
private Date startDate;
private Long countryId;
private Set<Long> resourceIds;
}
package cz.muni.fi.pa165.facade;
import cz.muni.fi.pa165.dto.AgentCountDto;
import cz.muni.fi.pa165.dto.AgentDetailDto;
import cz.muni.fi.pa165.dto.AgentListDto;
import cz.muni.fi.pa165.dto.SkillDto;
import cz.muni.fi.pa165.dto.*;
import org.springframework.data.domain.Page;
import java.util.List;
......@@ -18,4 +15,5 @@ public interface AgentFacade {
Page<AgentListDto> getAllAgents(int page, int pageSize);
Page<SkillDto> getAllSkills(int page, int pageSize);
List<SkillDto> getAllSkills();
void updateCodename(AgentNameUpdateDto agentNameUpdateDto);
}
package cz.muni.fi.pa165.facade;
import cz.muni.fi.pa165.dto.MissionCreateDto;
import cz.muni.fi.pa165.dto.MissionDetailDto;
import cz.muni.fi.pa165.dto.MissionListDto;
import org.springframework.data.domain.Page;
......@@ -8,4 +9,5 @@ public interface MissionFacade {
Page<MissionListDto> getAgentMissions(Long agentId, int page, int pageSize);
MissionDetailDto getMission(Long missionId);
Page<MissionListDto> getAllMissions(int page, int pageSize);
void createMission(MissionCreateDto missionCreateDto);
}
package cz.muni.fi.pa165.rest.controller;
import cz.muni.fi.pa165.dto.AgentCountDto;
import cz.muni.fi.pa165.dto.AgentDetailDto;
import cz.muni.fi.pa165.dto.AgentListDto;
import cz.muni.fi.pa165.dto.SkillDto;
import cz.muni.fi.pa165.dto.*;
import cz.muni.fi.pa165.facade.AgentFacade;
import cz.muni.fi.pa165.rest.ApiResponse;
import cz.muni.fi.pa165.rest.ApiUris;
......@@ -13,11 +10,12 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import java.util.List;
......@@ -185,4 +183,19 @@ public class AgentController {
return ResponseEntity.notFound().build();
}
}
@PutMapping("/codename")
@ResponseStatus(HttpStatus.OK)
public void updateName(@RequestBody AgentNameUpdateDto agentNameUpdateDto) {
agentFacade.updateCodename(agentNameUpdateDto);
}
@ExceptionHandler({ IllegalArgumentException.class })
public ResponseEntity<Object> handleInvalidInput(
IllegalArgumentException ex, WebRequest request) {
String error = ex.getMessage();
return new ResponseEntity<>(
error, new HttpHeaders(), HttpStatus.BAD_REQUEST);
}
}
package cz.muni.fi.pa165.rest.controller;
import cz.muni.fi.pa165.dto.CountryNameDto;
import cz.muni.fi.pa165.dto.MissionDetailDto;
import cz.muni.fi.pa165.dto.MissionListDto;
import cz.muni.fi.pa165.dto.SkillDto;
import cz.muni.fi.pa165.dto.*;
import cz.muni.fi.pa165.facade.MissionFacade;
import cz.muni.fi.pa165.rest.ApiResponse;
import cz.muni.fi.pa165.rest.ApiUris;
......@@ -13,11 +10,9 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import java.util.List;
......@@ -83,4 +78,10 @@ public class MissionController {
}
}
@PostMapping()
@ResponseStatus(HttpStatus.CREATED)
public void create(@RequestBody MissionCreateDto missionCreateDto) {
missionFacade.createMission(missionCreateDto);
}
}
......@@ -3,16 +3,14 @@ package cz.muni.fi.pa165.facade;
import cz.muni.fi.pa165.config.AgentDetailMapper;
import cz.muni.fi.pa165.config.AgentListMapper;
import cz.muni.fi.pa165.config.SkillMapper;
import cz.muni.fi.pa165.dto.AgentCountDto;
import cz.muni.fi.pa165.dto.AgentDetailDto;
import cz.muni.fi.pa165.dto.AgentListDto;
import cz.muni.fi.pa165.dto.SkillDto;
import cz.muni.fi.pa165.dto.*;
import cz.muni.fi.pa165.entity.*;
import cz.muni.fi.pa165.service.AgentService;
import cz.muni.fi.pa165.service.CountryService;
import cz.muni.fi.pa165.service.MissionService;
import cz.muni.fi.pa165.service.SkillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
......@@ -162,6 +160,26 @@ public class AgentFacadeImpl implements AgentFacade {
return skills.stream().map(skillMapper::map).collect(Collectors.toList());
}
@Override
public void updateCodename(AgentNameUpdateDto agentNameUpdateDto) {
if (agentNameUpdateDto.getCodename() == null || agentNameUpdateDto.getCodename().trim().equals("")) {
throw new IllegalArgumentException("Codename must not be empty.");
}
if (agentNameUpdateDto.getId() == null) {
throw new IllegalArgumentException("Id must not be null");
}
Agent agent = agentService.findById(agentNameUpdateDto.getId());
try {
agent.setCodename(agentNameUpdateDto.getCodename());
agentService.update(agent);
} catch (DataIntegrityViolationException exception) {
throw new IllegalArgumentException("Codename must be unique.");
}
}
private Page<AgentListDto> createPage(List<Agent> agents, Pageable pageable) {
List<AgentListDto> mappedAgents = agents.stream().map(agentListMapper::map).collect(Collectors.toList());
final int start = (int) pageable.getOffset();
......
......@@ -2,13 +2,17 @@ package cz.muni.fi.pa165.facade;
import cz.muni.fi.pa165.config.MissionDetailMapper;
import cz.muni.fi.pa165.config.MissionListMapper;
import cz.muni.fi.pa165.dto.MissionCreateDto;
import cz.muni.fi.pa165.dto.MissionDetailDto;
import cz.muni.fi.pa165.dto.MissionListDto;
import cz.muni.fi.pa165.entity.Agent;
import cz.muni.fi.pa165.entity.AgentMissionAssignment;
import cz.muni.fi.pa165.entity.Country;
import cz.muni.fi.pa165.entity.Mission;
import cz.muni.fi.pa165.entity.Resource;
import cz.muni.fi.pa165.service.AgentMissionAssignmentService;
import cz.muni.fi.pa165.service.CountryService;
import cz.muni.fi.pa165.service.MissionService;
import cz.muni.fi.pa165.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
......@@ -16,7 +20,9 @@ import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
......@@ -25,14 +31,18 @@ import java.util.stream.Collectors;
@Service
public class MissionFacadeImpl implements MissionFacade {
private final MissionService missionService;
private final CountryService countryService;
private final ResourceService resourceService;
private final AgentMissionAssignmentService agentMissionAssignmentService;
private final MissionListMapper missionListMapper;
private final MissionDetailMapper missionDetailMapper;
@Autowired
public MissionFacadeImpl(MissionService missionService, AgentMissionAssignmentService agentMissionAssignmentService, MissionListMapper missionListMapper, MissionDetailMapper missionDetailMapper) {
public MissionFacadeImpl(MissionService missionService, CountryService countryService, ResourceService resourceService, AgentMissionAssignmentService agentMissionAssignmentService, MissionListMapper missionListMapper, MissionDetailMapper missionDetailMapper) {
this.missionService = missionService;
this.countryService = countryService;
this.resourceService = resourceService;
this.agentMissionAssignmentService = agentMissionAssignmentService;
this.missionListMapper = missionListMapper;
this.missionDetailMapper = missionDetailMapper;
......@@ -65,4 +75,40 @@ public class MissionFacadeImpl implements MissionFacade {
Page<Mission> missions = missionService.findAll(pageable);
return missions.map(missionListMapper::map);
}
@Override
public void createMission(MissionCreateDto missionCreateDto) {
Mission mission = new Mission();
if (missionCreateDto.getCountryId() == null) {
throw new IllegalArgumentException("Country ID can not be null");
}
Country country = countryService.findById(missionCreateDto.getCountryId());
if (country == null) {
throw new IllegalArgumentException("Country with ID " + missionCreateDto.getCountryId() + " does not exist.");
}
Set<Long> resourceIds = missionCreateDto.getResourceIds() == null
? Set.of()
: missionCreateDto.getResourceIds();
Set<Resource> resources = new HashSet<>();
for (Long id : resourceIds) {
Resource resource = resourceService.findById(id);
if (resource == null) {
throw new IllegalArgumentException("Resource with ID " + id + " does not exist.");
}
resources.add(resource);
}
mission.setCountry(country);
mission.setResources(resources);
mission.setObjectives(missionCreateDto.getObjectives());
mission.setCodename(missionCreateDto.getCodename());
mission.setStartDate(missionCreateDto.getStartDate());
mission.setDurationDays(missionCreateDto.getDurationDays());
missionService.create(mission);
}
}
Supports Markdown
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