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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<artifactId>taskloom-backend</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>taskloom</name>
<description>Test-Driven Backend with Spring Boot</description>
<description>Less chaos, more completion - a human friendly task tracker</description>
<url/>
<licenses>
<license/>
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/taskloom/config/JpaAuditingConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.taskloom.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

@Configuration
@EnableJpaAuditing
public class JpaAuditingConfig {
}
2 changes: 1 addition & 1 deletion src/main/java/com/taskloom/controller/TaskController.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public ResponseEntity<TaskResponse> updateTask(@PathVariable Integer id, @Reques
@DeleteMapping("/{id}")
public ResponseEntity<String> deleteTaskById(@PathVariable Integer id) {
taskService.deleteTaskById(id);
return ResponseEntity.status(201).body(id + " was deleted");
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
}

@PatchMapping("/{id}/status")
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/taskloom/entity/TaskEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@
import com.taskloom.model.TaskStatus;
import jakarta.persistence.*;
import lombok.*;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.Instant;

@Getter
@Setter
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Builder
@EntityListeners(AuditingEntityListener.class)
public class TaskEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Expand All @@ -19,4 +25,12 @@ public class TaskEntity {

@Enumerated(EnumType.STRING)
private TaskStatus status = TaskStatus.TODO;

@CreatedDate
@Column(nullable = false, updatable = false)
private Instant createdAt;

@LastModifiedDate
@Column(nullable = false)
private Instant updatedAt;
}
4 changes: 4 additions & 0 deletions src/main/java/com/taskloom/model/response/TaskResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.time.Instant;

@Getter
@Setter
@AllArgsConstructor
Expand All @@ -15,4 +17,6 @@ public class TaskResponse {
private String title;
private String description;
private TaskStatus status;
private Instant createdAt;
private Instant updatedAt;
}
54 changes: 12 additions & 42 deletions src/main/java/com/taskloom/service/TaskService.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
public class TaskService {
private final TaskRepository taskRepository;

private TaskResponse toResponse(TaskEntity e){
return new TaskResponse(e.getId(), e.getTitle(), e.getDescription(), e.getStatus(), e.getCreatedAt(), e.getUpdatedAt());
}

public List<TaskResponse> findAll() {
List<TaskEntity> taskEntities = taskRepository.findAll();
Expand All @@ -27,46 +30,25 @@ public List<TaskResponse> findAll() {
}

return taskEntities.stream()
.map(taskEntity -> new TaskResponse(
taskEntity.getId(),
taskEntity.getTitle(),
taskEntity.getDescription(),
taskEntity.getStatus()
)).toList();
.map(this::toResponse)
.toList();
}

public TaskResponse findById(Integer id) {
TaskEntity taskEntity = taskRepository.findById(id)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Task not found"));

return new TaskResponse(
taskEntity.getId(),
taskEntity.getTitle(),
taskEntity.getDescription(),
taskEntity.getStatus()
);
return toResponse(taskEntity);
}

public TaskResponse createTask(TaskCreateRequest taskCreateRequest) {
TaskEntity taskEntity = TaskEntity.builder()
.title(taskCreateRequest.getTitle())
.description(taskCreateRequest.getDescription())
.status(taskCreateRequest.getStatus() != null ? taskCreateRequest.getStatus() : TaskStatus.TODO)
.build();

if(taskCreateRequest.getStatus() != null){
taskEntity.setStatus(taskCreateRequest.getStatus());
}
else{
taskEntity.setStatus(TaskStatus.TODO);
}

TaskEntity savedTask = taskRepository.save(taskEntity);
return new TaskResponse(
savedTask.getId(),
savedTask.getTitle(),
savedTask.getDescription(),
savedTask.getStatus()
);
return toResponse(taskRepository.save(taskEntity));
}

public TaskResponse updateTask(Integer id, TaskUpdateRequest taskUpdateRequest) {
Expand All @@ -75,32 +57,20 @@ public TaskResponse updateTask(Integer id, TaskUpdateRequest taskUpdateRequest)

taskEntity.setTitle(taskUpdateRequest.getTitle());
taskEntity.setDescription(taskUpdateRequest.getDescription());
taskEntity.setStatus(taskUpdateRequest.getStatus());
TaskEntity updatedTask = taskRepository.save(taskEntity);
taskEntity.setStatus(taskUpdateRequest.getStatus() != null ? taskUpdateRequest.getStatus() : TaskStatus.TODO);

return new TaskResponse(
updatedTask.getId(),
updatedTask.getTitle(),
updatedTask.getDescription(),
updatedTask.getStatus()
);
return toResponse(taskRepository.save(taskEntity));
}

public void deleteTaskById(Integer id) {
if(!taskRepository.existsById(id)) throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Task not found");
taskRepository.deleteById(id);
}

public TaskResponse updateTaskStatusById(Integer id, TaskStatusUpdate status) {
TaskEntity taskEntity = taskRepository.findById(id)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Task not found"));
taskEntity.setStatus(status.getTaskStatus());
TaskEntity updatedTask = taskRepository.save(taskEntity);

return new TaskResponse(
updatedTask.getId(),
updatedTask.getTitle(),
updatedTask.getDescription(),
updatedTask.getStatus()
);
return toResponse(taskRepository.save(taskEntity));
}
}
6 changes: 5 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ spring.h2.console.enabled=true
debug=true

spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.show-sql=true

# Hibernate JDBC oturum saat dilimi (DB'ye UTC gönder/DB'den UTC al)
spring.jpa.properties.hibernate.jdbc.time_zone=UTC
spring.jackson.time-zone=UTC
Loading