Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#16] Task CRUD DB 접근 로직 구현 및 core, storage 병합 #18

Merged
merged 8 commits into from
Sep 29, 2024
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
1 change: 1 addition & 0 deletions api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ dependencies {
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.restdocs:spring-restdocs-webtestclient'
testRuntimeOnly 'com.h2database:h2'
}

ext {
Expand Down
2 changes: 1 addition & 1 deletion api/src/docs/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
:icons: font
:source-highlighter: highlightjs
:toc: left
:toclevels: 1
:toclevels: 4
:sectlinks:
:docinfo: shared-head

Expand Down
7 changes: 5 additions & 2 deletions api/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
spring:
application:
name: api
profiles:
active: local
group:
local:
- storage
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
spring:
application:
name: storage
h2:
console:
enabled: true
Expand Down
2 changes: 2 additions & 0 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ dependencyManagement {

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'com.mysql:mysql-connector-j'
testImplementation 'org.junit.jupiter:junit-jupiter:5.10.3'
testImplementation 'org.mockito:mockito-core:5.12.0'
testImplementation 'org.assertj:assertj-core:3.24.2'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.taskbuddy.core.database.entity;

import com.taskbuddy.core.domain.Task;
import com.taskbuddy.core.domain.TimeFrame;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "tasks")
@Entity
public class TaskEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String title;

private Boolean isDone;

private String description;

private LocalDateTime startDateTime;

private LocalDateTime endDateTime;

private LocalDateTime createdAt;

private LocalDateTime updatedAt;

@Builder
private TaskEntity(Long id, String title, Boolean isDone, String description, LocalDateTime startDateTime, LocalDateTime endDateTime, LocalDateTime createdAt, LocalDateTime updatedAt) {
this.id = id;
this.title = title;
this.isDone = isDone;
this.description = description;
this.startDateTime = startDateTime;
this.endDateTime = endDateTime;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
}

public static TaskEntity from(Task task) {
return TaskEntity.builder()
.id(task.getId())
.title(task.getTitle())
.isDone(task.getIsDone())
.description(task.getDescription())
.startDateTime(task.getTimeFrame().startDateTime())
.endDateTime(task.getTimeFrame().endDateTime())
.createdAt(task.getCreatedAt())
.updatedAt(task.getUpdatedAt())
.build();
}

public Task toModel() {
return Task.builder()
.id(id)
.title(title)
.isDone(isDone)
.description(description)
.timeFrame(new TimeFrame(startDateTime, endDateTime))
.createdAt(createdAt)
.updatedAt(updatedAt)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.taskbuddy.core.database.repository;

import com.taskbuddy.core.database.entity.TaskEntity;
import com.taskbuddy.core.domain.Task;
import com.taskbuddy.core.service.port.TaskRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@RequiredArgsConstructor
@Repository
public class DefaultTaskRepository implements TaskRepository {
private final TaskJpaRepository taskJpaRepository;

@Override
public boolean existsById(Long id) {
return taskJpaRepository.existsById(id);
}

@Override
public Optional<Task> findById(Long id) {
return taskJpaRepository.findById(id)
.map(TaskEntity::toModel);
}

@Override
public Task save(Task task) {
return taskJpaRepository.save(TaskEntity.from(task))
.toModel();
}

@Override
public void deleteById(Long id) {
taskJpaRepository.deleteById(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.taskbuddy.core.database.repository;

import com.taskbuddy.core.database.entity.TaskEntity;
import org.springframework.data.jpa.repository.JpaRepository;

public interface TaskJpaRepository extends JpaRepository<TaskEntity, Long> {

}

This file was deleted.

9 changes: 9 additions & 0 deletions core/src/main/resources/application-storage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
spring:
config:
activate:
on-profile: storage
datasource:
url: jdbc:mysql://localhost:3306/taskbuddy
username: ${DB_USERNAME}
password: ${DB_PASSWORD}
driver-class-name: com.mysql.cj.jdbc.Driver
3 changes: 0 additions & 3 deletions core/src/main/resources/application.yml

This file was deleted.

12 changes: 12 additions & 0 deletions core/src/main/resources/ddl/create_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
create database taskbuddy default character set utf8mb4 collate utf8mb4_general_ci;

create table tasks (
id bigint not null auto_increment primary key,
title varchar(100) not null,
is_done boolean not null default false,
description varchar(300),
start_date_time timestamp not null,
end_date_time timestamp not null,
created_at timestamp not null default current_timestamp,
updated_at timestamp not null default current_timestamp
);
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ void setUp() {
//then
assertThat(result).isNotNull();
assertThat(result).isGreaterThan(0);
Mockito.verify(taskRepository, Mockito.times(1)).save(Mockito.any(Task.class));
}

@Test
Expand All @@ -123,6 +124,7 @@ void setUp() {
assertThatThrownBy(() -> taskService.updateContent(taskContentUpdate))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("The given task with id does not exist.");
Mockito.verify(taskRepository, Mockito.never()).save(Mockito.any(Task.class));
}

@Test
Expand Down Expand Up @@ -165,6 +167,7 @@ void setUp() {
assertThat(findTask.getTimeFrame().endDateTime()).isEqualTo(taskContentUpdate.endDateTime());
assertThat(findTask.getCreatedAt()).isEqualTo(givenCreatedDateTime);
assertThat(findTask.getUpdatedAt()).isEqualTo(currentDateTime);
Mockito.verify(taskRepository, Mockito.times(1)).save(Mockito.any(Task.class));
}

@Test
Expand All @@ -177,6 +180,7 @@ void setUp() {
assertThatThrownBy(() -> taskService.deleteTask(givenId))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("The given task with id does not exist.");
Mockito.verify(taskRepository, Mockito.never()).deleteById(givenId);
}

@Test
Expand All @@ -189,6 +193,6 @@ void setUp() {
Assertions.assertDoesNotThrow(() -> taskService.deleteTask(givenId));

//then
Mockito.verify(taskRepository).deleteById(givenId);
Mockito.verify(taskRepository, Mockito.times(1)).deleteById(givenId);
}
}
1 change: 0 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ plugins {
rootProject.name = 'task-buddy'

include ':core'
include ':storage'
include ':api'
26 changes: 0 additions & 26 deletions storage/build.gradle

This file was deleted.

Loading