Skip to content

Commit

Permalink
Merge pull request #9 from szymonpoltorak/sprint-crud
Browse files Browse the repository at this point in the history
Sprint crud
  • Loading branch information
szymonpoltorak authored Dec 17, 2023
2 parents 20a19c2 + 71a41c7 commit ab627a5
Show file tree
Hide file tree
Showing 22 changed files with 1,111 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.corn.cornbackend.api.project.data;

import dev.corn.cornbackend.entities.sprint.data.SprintResponse;
import dev.corn.cornbackend.api.sprint.data.SprintResponse;
import lombok.Builder;

import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package dev.corn.cornbackend.api.sprint;

import dev.corn.cornbackend.api.sprint.data.SprintRequest;
import dev.corn.cornbackend.api.sprint.data.SprintResponse;
import dev.corn.cornbackend.api.sprint.interfaces.SprintController;
import dev.corn.cornbackend.api.sprint.interfaces.SprintService;
import dev.corn.cornbackend.config.jwtprocessing.JwtAuthed;
import dev.corn.cornbackend.entities.user.User;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.List;

import static dev.corn.cornbackend.api.sprint.constants.SprintMappings.ADD_SPRINT;
import static dev.corn.cornbackend.api.sprint.constants.SprintMappings.DELETE_SPRINT;
import static dev.corn.cornbackend.api.sprint.constants.SprintMappings.GET_SPRINTS_ON_PAGE;
import static dev.corn.cornbackend.api.sprint.constants.SprintMappings.GET_SPRINT_BY_ID;
import static dev.corn.cornbackend.api.sprint.constants.SprintMappings.SPRINT_API_ENDPOINT;
import static dev.corn.cornbackend.api.sprint.constants.SprintMappings.UPDATE_SPRINTS_DESCRIPTION;
import static dev.corn.cornbackend.api.sprint.constants.SprintMappings.UPDATE_SPRINTS_END_DATE;
import static dev.corn.cornbackend.api.sprint.constants.SprintMappings.UPDATE_SPRINTS_NAME;
import static dev.corn.cornbackend.api.sprint.constants.SprintMappings.UPDATE_SPRINTS_START_DATE;

@RestController
@RequiredArgsConstructor
@RequestMapping(value = SPRINT_API_ENDPOINT)
public class SprintControllerImpl implements SprintController {

private final SprintService sprintService;

@Override
@PostMapping(value = ADD_SPRINT)
public SprintResponse addNewSprint(@RequestBody SprintRequest sprintRequest, @JwtAuthed User user) {
return sprintService.addNewSprint(sprintRequest, user);
}

@Override
@GetMapping(value = GET_SPRINT_BY_ID)
public SprintResponse getSprintById(@RequestParam long sprintId, @JwtAuthed User user) {
return sprintService.getSprintById(sprintId, user);
}

@Override
@GetMapping(value = GET_SPRINTS_ON_PAGE)
public List<SprintResponse> getSprintsOnPage(@RequestParam int page, @RequestParam long projectId, @JwtAuthed User user) {
return sprintService.getSprintsOnPage(page, projectId, user);
}

@Override
@PatchMapping(value = UPDATE_SPRINTS_NAME)
public SprintResponse updateSprintsName(@RequestParam String name, @RequestParam long sprintId, @JwtAuthed User user) {
return sprintService.updateSprintsName(name, sprintId, user);
}

@Override
@PatchMapping(value = UPDATE_SPRINTS_DESCRIPTION)
public SprintResponse updateSprintsDescription(@RequestParam String description, @RequestParam long sprintId, @JwtAuthed User user) {
return sprintService.updateSprintsDescription(description, sprintId, user);
}

@Override
@PatchMapping(value = UPDATE_SPRINTS_START_DATE)
public SprintResponse updateSprintsStartDate(@RequestParam LocalDate startDate, @RequestParam long sprintId, @JwtAuthed User user) {
return sprintService.updateSprintsStartDate(startDate, sprintId, user);
}

@Override
@PatchMapping(value = UPDATE_SPRINTS_END_DATE)
public SprintResponse updateSprintsEndDate(@RequestParam LocalDate endDate, @RequestParam long sprintId, @JwtAuthed User user) {
return sprintService.updateSprintsEndDate(endDate, sprintId, user);
}

@Override
@DeleteMapping(value = DELETE_SPRINT)
public SprintResponse deleteSprint(@RequestParam long sprintId, @JwtAuthed User user) {
return sprintService.deleteSprint(sprintId, user);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package dev.corn.cornbackend.api.sprint;

import dev.corn.cornbackend.api.sprint.data.SprintRequest;
import dev.corn.cornbackend.api.sprint.data.SprintResponse;
import dev.corn.cornbackend.api.sprint.interfaces.SprintService;
import dev.corn.cornbackend.entities.sprint.Sprint;
import dev.corn.cornbackend.entities.sprint.interfaces.SprintMapper;
import dev.corn.cornbackend.entities.sprint.interfaces.SprintRepository;
import dev.corn.cornbackend.entities.user.User;
import dev.corn.cornbackend.utils.exceptions.sprint.SprintDoesNotExistException;
import dev.corn.cornbackend.utils.exceptions.sprint.SprintEndDateMustBeAfterStartDate;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class SprintServiceImpl implements SprintService {
public static final int SPRINTS_PER_PAGE = 20;
private final SprintRepository sprintRepository;
private final SprintMapper sprintMapper;

@Override
public final SprintResponse addNewSprint(SprintRequest sprintRequest, User user) {
//auth user

if (sprintRequest.endDate().isBefore(sprintRequest.startDate())) {
throw new SprintEndDateMustBeAfterStartDate(sprintRequest.startDate(), sprintRequest.endDate());
}
Sprint sprint = Sprint
.builder()
.sprintName(sprintRequest.name())
.sprintStartDate(sprintRequest.startDate())
.sprintEndDate(sprintRequest.endDate())
.sprintDescription(sprintRequest.description())
.build();
log.info("Instantiated sprint: {}", sprint);

Sprint newSprint = sprintRepository.save(sprint);
log.info("Saved sprint: {}", sprint);

return sprintMapper.toSprintResponse(newSprint);
}

@Override
public final SprintResponse getSprintById(long sprintId, User user) {
log.info("Getting sprint with id: {} for user: {}", sprintId, user);

//auth user

Sprint sprint = sprintRepository.findById(sprintId)
.orElseThrow(() -> new SprintDoesNotExistException(sprintId));
log.info("Sprint found : {}", sprint);

return sprintMapper.toSprintResponse(sprint);
}

@Override
public final List<SprintResponse> getSprintsOnPage(int page, long projectId, User user) {
log.info("Getting sprints in project {} on page: {} for user: {}", projectId, page, user);

//auth user

Pageable pageable = PageRequest.of(page, SPRINTS_PER_PAGE);
Page<Sprint> sprints = sprintRepository.findAllByProjectId(projectId, pageable);
log.info("Sprints found on page : {}", sprints.getTotalElements());

return sprints.map(sprintMapper::toSprintResponse).toList();
}

@Override
public final SprintResponse updateSprintsName(String name, long sprintId, User user) {
log.info("Updating sprint with id: {} name to: {}", sprintId, name);

//auth user

Sprint sprintToUpdate = sprintRepository.findById(sprintId)
.orElseThrow(() -> new SprintDoesNotExistException(sprintId));
log.info("Found sprint to update: {}", sprintToUpdate);

sprintToUpdate.setSprintName(name);
Sprint updatedSprint = sprintRepository.save(sprintToUpdate);
log.info("Updated sprint: {}", updatedSprint);

return sprintMapper.toSprintResponse(updatedSprint);
}

@Override
public final SprintResponse updateSprintsDescription(String description, long sprintId, User user) {
log.info("Updating sprint with id: {} description to: {}", sprintId, description);

//auth user

Sprint sprintToUpdate = sprintRepository.findById(sprintId)
.orElseThrow(() -> new SprintDoesNotExistException(sprintId));
log.info("Found sprint to update: {}", sprintToUpdate);

sprintToUpdate.setSprintDescription(description);
Sprint updatedSprint = sprintRepository.save(sprintToUpdate);
log.info("Updated sprint: {}", updatedSprint);

return sprintMapper.toSprintResponse(updatedSprint);
}

@Override
public final SprintResponse updateSprintsStartDate(LocalDate startDate, long sprintId, User user) {
log.info("Updating sprint with id: {} startDate to: {}", sprintId, startDate);

//auth user

Sprint sprintToUpdate = sprintRepository.findById(sprintId)
.orElseThrow(() -> new SprintDoesNotExistException(sprintId));
log.info("Found sprint to update: {}", sprintToUpdate);

if (sprintToUpdate.isEndBefore(startDate)) {
throw new SprintEndDateMustBeAfterStartDate(startDate, sprintToUpdate.getSprintEndDate());
}

sprintToUpdate.setSprintStartDate(startDate);
Sprint updatedSprint = sprintRepository.save(sprintToUpdate);
log.info("Updated sprint: {}", updatedSprint);

return sprintMapper.toSprintResponse(updatedSprint);
}

@Override
public final SprintResponse updateSprintsEndDate(LocalDate endDate, long sprintId, User user) {
log.info("Updating sprint with id: {} endDate to: {}", sprintId, endDate);

//auth user

Sprint sprintToUpdate = sprintRepository.findById(sprintId)
.orElseThrow(() -> new SprintDoesNotExistException(sprintId));
log.info("Found sprint to update: {}", sprintToUpdate);

if (sprintToUpdate.isStartAfter(endDate)) {
throw new SprintEndDateMustBeAfterStartDate(sprintToUpdate.getSprintStartDate(), endDate);
}

sprintToUpdate.setSprintEndDate(endDate);
Sprint updatedSprint = sprintRepository.save(sprintToUpdate);
log.info("Updated sprint: {}", updatedSprint);

return sprintMapper.toSprintResponse(sprintToUpdate);
}

@Override
public final SprintResponse deleteSprint(long sprintId, User user) {
log.info("Deleting sprint with id: {}", sprintId);

//auth user

Sprint sprintToDelete = sprintRepository.findById(sprintId)
.orElseThrow(() -> new SprintDoesNotExistException(sprintId));
log.info("Found sprint to delete: {}", sprintToDelete);

sprintRepository.deleteById(sprintId);
log.info("Deleted sprint with id: {}", sprintToDelete);

return sprintMapper.toSprintResponse(sprintToDelete);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package dev.corn.cornbackend.api.sprint.constants;

public final class SprintMappings {

private SprintMappings() {
}

public static final String SPRINT_API_ENDPOINT = "/api/v1/sprint";

public static final String GET_SPRINTS_ON_PAGE = "/getSprintsOnPage";

public static final String GET_SPRINT_BY_ID = "/getSprintById";

public static final String ADD_SPRINT = "/addSprint";

public static final String UPDATE_SPRINTS_NAME = "/updateSprintsName";
public static final String UPDATE_SPRINTS_DESCRIPTION = "/updateSprintsName";
public static final String UPDATE_SPRINTS_START_DATE = "/updateSprintsName";
public static final String UPDATE_SPRINTS_END_DATE = "/updateSprintsName";

public static final String DELETE_SPRINT = "/deleteSprint";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dev.corn.cornbackend.api.sprint.data;

import lombok.Builder;

import java.time.LocalDate;

@Builder
public record SprintRequest(String name, LocalDate startDate, LocalDate endDate, String description) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package dev.corn.cornbackend.api.sprint.data;

import lombok.Builder;

import java.time.LocalDate;

@Builder
public record SprintResponse(long sprintId, long projectId, String sprintName, String sprintDescription,
LocalDate sprintStartDate, LocalDate sprintEndDate) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package dev.corn.cornbackend.api.sprint.interfaces;

import dev.corn.cornbackend.api.sprint.data.SprintRequest;
import dev.corn.cornbackend.api.sprint.data.SprintResponse;
import dev.corn.cornbackend.entities.project.Project;
import dev.corn.cornbackend.entities.user.User;

import java.time.LocalDate;
import java.util.List;

public interface SprintController {

SprintResponse addNewSprint(SprintRequest sprintRequest, User user);

SprintResponse getSprintById(long sprintId, User user);

List<SprintResponse> getSprintsOnPage(int page, long projectId, User user);

SprintResponse updateSprintsName(String name, long sprintId, User user);

SprintResponse updateSprintsDescription(String description, long sprintId, User user);

SprintResponse updateSprintsStartDate(LocalDate startDate, long sprintId, User user);

SprintResponse updateSprintsEndDate(LocalDate endDate, long sprintId, User user);

SprintResponse deleteSprint(long sprintId, User user);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package dev.corn.cornbackend.api.sprint.interfaces;

import dev.corn.cornbackend.api.sprint.data.SprintRequest;
import dev.corn.cornbackend.api.sprint.data.SprintResponse;
import dev.corn.cornbackend.entities.project.Project;
import dev.corn.cornbackend.entities.user.User;

import java.time.LocalDate;
import java.util.List;

public interface SprintService {

SprintResponse addNewSprint(SprintRequest sprintRequest, User user);

SprintResponse getSprintById(long sprintId, User user);

List<SprintResponse> getSprintsOnPage(int page, long projectId, User user);

SprintResponse updateSprintsName(String name, long sprintId, User user);

SprintResponse updateSprintsDescription(String description, long sprintId, User user);

SprintResponse updateSprintsStartDate(LocalDate startDate, long sprintId, User user);

SprintResponse updateSprintsEndDate(LocalDate endDate, long sprintId, User user);

SprintResponse deleteSprint(long sprintId, User user);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

import dev.corn.cornbackend.api.project.data.ProjectResponse;
import dev.corn.cornbackend.entities.project.Project;
import dev.corn.cornbackend.entities.sprint.Sprint;
import dev.corn.cornbackend.entities.sprint.data.SprintResponse;
import org.mapstruct.Mapper;

@Mapper(componentModel = "spring")
public interface ProjectMapper {
ProjectResponse toProjectResponse(Project project);

SprintResponse toSprintResponse(Sprint sprint);
}
Loading

0 comments on commit ab627a5

Please sign in to comment.