Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.14</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package com.jonassavas.spring_task_api.controllers;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.jonassavas.spring_task_api.domain.dto.TaskRequestDto;
import com.jonassavas.spring_task_api.domain.dto.TaskDto;
import com.jonassavas.spring_task_api.domain.entities.TaskEntity;
import com.jonassavas.spring_task_api.mappers.Mapper;
import com.jonassavas.spring_task_api.services.TaskGroupService;
import com.jonassavas.spring_task_api.services.TaskService;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand All @@ -21,19 +23,31 @@
public class TaskController {

private Mapper<TaskEntity, TaskDto> taskMapper;
private Mapper<TaskEntity, TaskRequestDto> taskRequestMapper;
private TaskService taskService;
private TaskGroupService taskGroupService;


public TaskController(Mapper<TaskEntity, TaskDto> taskMapper, TaskService taskService){
public TaskController(Mapper<TaskEntity, TaskDto> taskMapper,
TaskService taskService,
TaskGroupService taskGroupService,
Mapper<TaskEntity, TaskRequestDto> taskRequestMapper){
this.taskMapper = taskMapper;
this.taskService = taskService;
this.taskGroupService = taskGroupService;
this.taskRequestMapper = taskRequestMapper;
}


@PostMapping("/taskgroups/{groupId}/tasks")
public ResponseEntity<TaskDto> createTask(
@PathVariable Long groupId,
@RequestBody TaskDto dto) {

// Can't create a task without a valid task group
if(!taskGroupService.isExist(groupId)){
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}

TaskEntity taskEntity = taskMapper.mapFrom(dto);
TaskEntity savedTask = taskService.createTask(groupId, taskEntity);
Expand All @@ -43,8 +57,14 @@ public ResponseEntity<TaskDto> createTask(

@DeleteMapping(path = "/tasks/{id}")
public ResponseEntity deleteTask(@PathVariable("id") Long id){
taskService.deleteTask(id);
taskService.delete(id);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

@PatchMapping(path = "/tasks/{id}")
public ResponseEntity<TaskRequestDto> update(@PathVariable Long id, @RequestBody TaskRequestDto dto){
TaskEntity updated = taskService.update(id, dto);

return new ResponseEntity<>(taskRequestMapper.mapTo(updated), HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import org.springframework.web.bind.annotation.RestController;

import com.jonassavas.spring_task_api.domain.dto.CreateTaskGroupDto;
import com.jonassavas.spring_task_api.domain.dto.TaskGroupRequestDto;
import com.jonassavas.spring_task_api.domain.dto.TaskDto;
import com.jonassavas.spring_task_api.domain.dto.TaskGroupDto;
import com.jonassavas.spring_task_api.domain.dto.TaskGroupWithTasksDto;
import com.jonassavas.spring_task_api.domain.entities.TaskEntity;
import com.jonassavas.spring_task_api.domain.entities.TaskGroupEntity;
import com.jonassavas.spring_task_api.mappers.Mapper;
Expand All @@ -16,38 +17,88 @@

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;


@RestController
public class TaskGroupController {
private TaskGroupService taskGroupService;

private Mapper<TaskGroupEntity, CreateTaskGroupDto> createTaskGroupMapper;
private Mapper<TaskGroupEntity, TaskGroupRequestDto> taskGroupRequestMapper;
private Mapper<TaskGroupEntity, TaskGroupWithTasksDto> taskGroupWithTasksMapper;
private Mapper<TaskGroupEntity, TaskGroupDto> taskGroupMapper;

public TaskGroupController(TaskGroupService taskGroupService,
Mapper<TaskGroupEntity, CreateTaskGroupDto> createTaskGroupMapper,
Mapper<TaskGroupEntity, TaskGroupDto> taskGroupMapper){
Mapper<TaskGroupEntity, TaskGroupRequestDto> taskGroupRequestMapper,
Mapper<TaskGroupEntity, TaskGroupDto> taskGroupMapper,
Mapper<TaskGroupEntity, TaskGroupWithTasksDto> taskGroupWithTasksMapper){
this.taskGroupService = taskGroupService;
this.createTaskGroupMapper = createTaskGroupMapper;
this.taskGroupRequestMapper = taskGroupRequestMapper;
this.taskGroupMapper = taskGroupMapper;
this.taskGroupWithTasksMapper = taskGroupWithTasksMapper;
}

@PostMapping(path = "/taskgroups")
public ResponseEntity<CreateTaskGroupDto> createTaskGroup(@RequestBody CreateTaskGroupDto taskGroup) {
TaskGroupEntity taskGroupEntity = createTaskGroupMapper.mapFrom(taskGroup);
public ResponseEntity<TaskGroupRequestDto> createTaskGroup(@RequestBody TaskGroupRequestDto taskGroup) {
TaskGroupEntity taskGroupEntity = taskGroupRequestMapper.mapFrom(taskGroup);
TaskGroupEntity savedTaskGroupEntity = taskGroupService.save(taskGroupEntity);
return new ResponseEntity<>(createTaskGroupMapper.mapTo(savedTaskGroupEntity), HttpStatus.CREATED);
return new ResponseEntity<>(taskGroupRequestMapper.mapTo(savedTaskGroupEntity), HttpStatus.CREATED);
}

@GetMapping(path = "/taskgroups")
@GetMapping("/taskgroups")
public List<TaskGroupDto> listTaskGroups() {
List<TaskGroupEntity> taskGroups = taskGroupService.findAll();
return taskGroups.stream().map(taskGroupMapper::mapTo).collect(Collectors.toList());
return taskGroupService.findAll()
.stream()
.map(taskGroupMapper::mapTo)
.toList();
}

// @GetMapping("/taskgroups/{id}/tasks")
// public List<TaskDto> listTasksForGroup(@PathVariable Long id) {
// return taskService.findByGroupId(id)
// .stream()
// .map(taskMapper::mapTo)
// .toList();
// }

@GetMapping("/taskgroups/with-tasks")
public List<TaskGroupWithTasksDto> listTaskGroupsWithTasks() {
return taskGroupService.findAllWithTasks()
.stream()
.map(taskGroupWithTasksMapper::mapTo)
.toList();
}



// @GetMapping(path = "/taskgroups")
// public List<TaskGroupDto> listTaskGroups() {
// List<TaskGroupEntity> taskGroups = taskGroupService.findAll();
// return taskGroups.stream().map(taskGroupMapper::mapTo).collect(Collectors.toList());
// }

@DeleteMapping(path = "/taskgroups/{id}")
public ResponseEntity deleteTaskGroup(@PathVariable("id") Long id){
taskGroupService.delete(id);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

@DeleteMapping(path = "/taskgroups/{groupId}/tasks")
public ResponseEntity deleteAllTasks(@PathVariable("groupId") Long groupId){
taskGroupService.deleteAllTasks(groupId);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

@PatchMapping(path = "/taskgroups/{id}")
public ResponseEntity<TaskGroupRequestDto> updateTaskGroup(@PathVariable Long id, @RequestBody TaskGroupRequestDto dto){
TaskGroupEntity updated = taskGroupService.update(id, dto);
return new ResponseEntity<>(taskGroupRequestMapper.mapTo(updated), HttpStatus.OK);
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package com.jonassavas.spring_task_api.domain.dto;

import java.util.List;

import com.jonassavas.spring_task_api.domain.entities.TaskEntity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand All @@ -18,5 +14,4 @@ public class TaskGroupDto {

private String taskGroupName;

private List<TaskEntity> tasks;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.jonassavas.spring_task_api.domain.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;



/*
USED FOR:
- CREATE
- UPDATE

This class serves as the DTO for incoming and outgoing requests.
This means that when the user wants to create/change any parameter
of their TaskGroup they do it via this DTO.
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class TaskGroupRequestDto {
private String taskGroupName;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.jonassavas.spring_task_api.domain.dto;

import java.util.List;

import com.jonassavas.spring_task_api.domain.entities.TaskEntity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand All @@ -9,10 +13,10 @@
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class CreateTaskGroupDto {
public class TaskGroupWithTasksDto {
private Long id;

private String taskGroupName;

//private List<TaskEntity> tasks; //We don't allow tasks on create
}
private List<TaskDto> tasks;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.jonassavas.spring_task_api.domain.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class TaskRequestDto {
private Long taskGroupId;
private String taskName;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@
import org.springframework.stereotype.Component;


import com.jonassavas.spring_task_api.domain.dto.CreateTaskGroupDto;
import com.jonassavas.spring_task_api.domain.dto.TaskGroupRequestDto;
import com.jonassavas.spring_task_api.domain.entities.TaskGroupEntity;
import com.jonassavas.spring_task_api.mappers.Mapper;

@Component
public class CreateTaskGroupMapper implements Mapper<TaskGroupEntity, CreateTaskGroupDto>{
public class TaskGroupRequestMapper implements Mapper<TaskGroupEntity, TaskGroupRequestDto>{
private ModelMapper modelMapper;

public CreateTaskGroupMapper(ModelMapper modelMapper){
public TaskGroupRequestMapper(ModelMapper modelMapper){
this.modelMapper = modelMapper;
}

@Override
public CreateTaskGroupDto mapTo(TaskGroupEntity taskGroupEntity){
return modelMapper.map(taskGroupEntity, CreateTaskGroupDto.class);
public TaskGroupRequestDto mapTo(TaskGroupEntity taskGroupEntity){
return modelMapper.map(taskGroupEntity, TaskGroupRequestDto.class);
}

@Override
public TaskGroupEntity mapFrom(CreateTaskGroupDto taskGroupDto){
public TaskGroupEntity mapFrom(TaskGroupRequestDto taskGroupDto){
return modelMapper.map(taskGroupDto, TaskGroupEntity.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.jonassavas.spring_task_api.mappers.impl;

import org.modelmapper.ModelMapper;
import org.springframework.stereotype.Component;


import com.jonassavas.spring_task_api.domain.dto.TaskGroupWithTasksDto;
import com.jonassavas.spring_task_api.domain.entities.TaskGroupEntity;
import com.jonassavas.spring_task_api.mappers.Mapper;

@Component
public class TaskGroupWithTasksMapper implements Mapper<TaskGroupEntity, TaskGroupWithTasksDto>{
private ModelMapper modelMapper;

public TaskGroupWithTasksMapper(ModelMapper modelMapper){
this.modelMapper = modelMapper;
}

@Override
public TaskGroupWithTasksDto mapTo(TaskGroupEntity taskGroupEntity){
return modelMapper.map(taskGroupEntity, TaskGroupWithTasksDto.class);
}

@Override
public TaskGroupEntity mapFrom(TaskGroupWithTasksDto taskGroupDto){
return modelMapper.map(taskGroupDto, TaskGroupEntity.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.jonassavas.spring_task_api.mappers.impl;

import org.modelmapper.ModelMapper;
import org.springframework.stereotype.Component;

import com.jonassavas.spring_task_api.domain.dto.TaskRequestDto;
import com.jonassavas.spring_task_api.domain.dto.TaskDto;
import com.jonassavas.spring_task_api.domain.entities.TaskEntity;
import com.jonassavas.spring_task_api.mappers.Mapper;

@Component
public class TaskRequestMapper implements Mapper<TaskEntity, TaskRequestDto> {

private ModelMapper modelMapper;

public TaskRequestMapper(ModelMapper modelMapper) {
this.modelMapper = modelMapper;

// Skip taskGroup when mapping DTO -> Entity
this.modelMapper.typeMap(TaskRequestDto.class, TaskEntity.class)
.addMappings(mapper -> mapper.skip(TaskEntity::setTaskGroup));
}

@Override
public TaskRequestDto mapTo(TaskEntity taskEntity) {
TaskRequestDto dto = modelMapper.map(taskEntity, TaskRequestDto.class);
//dto.setTaskGroupId(taskEntity.getTaskGroup().getId());
return dto;
}

@Override
public TaskEntity mapFrom(TaskRequestDto createTaskDto) {
return modelMapper.map(createTaskDto, TaskEntity.class);
}
}

Loading