Skip to content

Commit

Permalink
Merge pull request #4 from gooiman/feat/메모수정
Browse files Browse the repository at this point in the history
Feat 메모수정
  • Loading branch information
koosco authored Sep 28, 2024
2 parents ba4f78d + 7ec8fae commit 487da1d
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 10 deletions.
71 changes: 70 additions & 1 deletion sql/init.sql
Original file line number Diff line number Diff line change
@@ -1 +1,70 @@
CREATE DATABASE gooiman;
CREATE DATABASE gooiman;

drop table if exists memo;
drop table if exists users;
drop table if exists page;

CREATE TABLE page
(
page_id BINARY(16) NOT NULL PRIMARY KEY,
page_name VARCHAR(255) NOT NULL
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4;


CREATE TABLE users
(
user_id BINARY(16) NOT NULL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL,
page_id BINARY(16),
CONSTRAINT fk_users_page FOREIGN KEY (page_id) REFERENCES page (page_id)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4;


CREATE TABLE memo
(
memo_id BINARY(16) NOT NULL PRIMARY KEY,
category VARCHAR(255),
sub_category VARCHAR(255),
title VARCHAR(255) NOT NULL,
content TEXT,
page_id BINARY(16),
user_id BINARY(16),
CONSTRAINT fk_memo_page FOREIGN KEY (page_id) REFERENCES page (page_id),
CONSTRAINT fk_memo_user FOREIGN KEY (user_id) REFERENCES users (user_id)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4;

-- page 더미
INSERT INTO page (page_id, page_name)
VALUES (UUID_TO_BIN('dbdfa00c-4292-48c2-92b4-97c643e6dd5a'), 'page1');
-- (UUID_TO_BIN('dbee3cdf-20e1-4ddb-a510-3cf5909f875c'), 'page2'),
-- (UUID_TO_BIN('1cf08322-da12-4838-a1ea-3e45ffc701a6'), 'page3');

-- user1 더미 추가
INSERT INTO users (user_id, name, password, page_id)
VALUES (UUID_TO_BIN('382afa8a-79d2-4fe8-9f61-2c4db816ee3a'), 'user1', '1234',
UUID_TO_BIN('dbdfa00c-4292-48c2-92b4-97c643e6dd5a'));
-- (UUID_TO_BIN('082e940e-cd05-49b1-b5e5-e62eeb3a478b'), 'Bob', 'password456', NULL),
-- (UUID_TO_BIN('41f69597-45d2-4732-8c4b-68775cbf7468'), 'Charlie', 'password789', NULL);

-- user1의 memo 추가
INSERT INTO memo (memo_id, category, sub_category, title, content, page_id, user_id)
VALUES (UUID_TO_BIN('24a4aea3-eb63-475c-b9e3-212d58986980'), 'category1', 'sub_category1', 'title1',
'content1.', UUID_TO_BIN('dbdfa00c-4292-48c2-92b4-97c643e6dd5a'),
UUID_TO_BIN('382afa8a-79d2-4fe8-9f61-2c4db816ee3a')),
(UUID_TO_BIN('4bb960d6-4dc1-4b31-a3c7-333c5bd589d2'), 'category1', 'sub_category2', 'title2',
'content2', UUID_TO_BIN('dbdfa00c-4292-48c2-92b4-97c643e6dd5a'),
UUID_TO_BIN('382afa8a-79d2-4fe8-9f61-2c4db816ee3a')),
(UUID_TO_BIN('cfda99e4-2d9f-4188-9498-4dea0bdb9011'), 'category1', 'sub_category3', 'title3',
'3', UUID_TO_BIN('dbdfa00c-4292-48c2-92b4-97c643e6dd5a'),
UUID_TO_BIN('382afa8a-79d2-4fe8-9f61-2c4db816ee3a'));

select *
from page;
select *
from users;
select *
from memo;
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ public enum ErrorCode {
NOT_FOUND_RESOURCE(40400, HttpStatus.NOT_FOUND, "해당 리소스가 존재하지 않습니다."),
NOT_FOUND_LOGIN_USER(40401, HttpStatus.NOT_FOUND, "로그인한 사용자가 존재하지 않습니다."),
NOT_FOUND_AUTHORIZATION_HEADER(40401, HttpStatus.NOT_FOUND, "Authorization 헤더가 존재하지 않습니다."),
NOT_FOUND_MEMBER(40402, HttpStatus.NOT_FOUND, "해당 사용자가 존재하지 않습니다."),
NOT_FOUND_USER(40402, HttpStatus.NOT_FOUND, "해당 사용자가 존재하지 않습니다."),
NOT_FOUND_SHARED_URL(40403, HttpStatus.NOT_FOUND, "해당 공유 URL이 존재하지 않습니다."),
NOT_FOUND_MEMO(40404, HttpStatus.NOT_FOUND, "해당 메모가 존재하지 않습니다"),


// Invalid Argument Error
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/dev/gooiman/server/memo/application/MemoService.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
package dev.gooiman.server.memo.application;


import dev.gooiman.server.common.dto.CommonSuccessDto;
import dev.gooiman.server.common.exception.CommonException;
import dev.gooiman.server.common.exception.ErrorCode;
import dev.gooiman.server.memo.application.dto.UpdateMemoRequestDto;
import dev.gooiman.server.memo.repository.MemoRepository;
import dev.gooiman.server.memo.repository.entity.Memo;
import dev.gooiman.server.page.repository.PageRepository;
import dev.gooiman.server.user.application.UserService;
import dev.gooiman.server.user.repository.UserRepository;
import dev.gooiman.server.user.repository.entity.User;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class MemoService {

private final MemoRepository memoRepository;
private final UserService userService;
private final UserRepository userRepository;
private final PageRepository pageRepository;

Expand Down Expand Up @@ -46,6 +58,17 @@ public class MemoService {
// CreateMemoResponseDto.Res res = CreateMemoResponseDto.Res.mapEntityToDto(savedMemo);
// return res;
// }

@Transactional
public CommonSuccessDto updateMemo(String memoId, @RequestBody UpdateMemoRequestDto dto) {
UUID uuid = UUID.fromString(memoId);
Memo memo = memoRepository.findById(uuid)
.orElseThrow(() -> new CommonException(ErrorCode.NOT_MATCH_AUTH_CODE));
User user = userService.getUserByName(dto.author());
memo.updateInfo(dto.title(), dto.content(), dto.category(), dto.subCategory(), user);

return CommonSuccessDto.fromEntity(true);
}
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dev.gooiman.server.memo.application.dto;

public record UpdateMemoRequestDto(String title,
String content,
String author,
String category,
String subCategory) {

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package dev.gooiman.server.memo.controller;

import dev.gooiman.server.common.dto.CommonSuccessDto;
import dev.gooiman.server.common.dto.ResponseDto;
import dev.gooiman.server.memo.application.MemoService;
import dev.gooiman.server.memo.application.dto.UpdateMemoRequestDto;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

Expand All @@ -25,4 +31,10 @@ public class MemoController {
// }
// return null;
// }

@PatchMapping("/{memoId}")
public ResponseDto<CommonSuccessDto> updateMemo(@PathVariable("memoId") String memoId,
@RequestBody UpdateMemoRequestDto dto) {
return ResponseDto.ok(memoService.updateMemo(memoId, dto));
}
}
16 changes: 16 additions & 0 deletions src/main/java/dev/gooiman/server/memo/repository/entity/Memo.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,24 @@
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.Lob;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.GenericGenerator;

@Getter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Entity
@Table(name = "memo")
@DynamicUpdate
public class Memo {

@Id
Expand All @@ -31,6 +36,9 @@ public class Memo {
private String category;
private String subCategory;
private String title;

@Lob
@Column(columnDefinition = "TEXT")
private String content;

@ManyToOne(fetch = FetchType.LAZY)
Expand All @@ -41,4 +49,12 @@ public class Memo {
@JoinColumn(name = "USER_ID")
private User user;

public void updateInfo(String title, String content, String category, String subCategory,
User user) {
this.title = title;
this.content = content;
this.category = category;
this.subCategory = subCategory;
this.user = user;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package dev.gooiman.server.page.repository.entity;

import dev.gooiman.server.memo.repository.entity.Memo;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import java.util.ArrayList;
import java.util.List;
import jakarta.persistence.Table;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand All @@ -20,6 +17,7 @@
@AllArgsConstructor
@Builder
@Entity
@Table(name = "page")
public class Page {

@Id
Expand All @@ -30,6 +28,6 @@ public class Page {

private String pageName;

@OneToMany(mappedBy = "page")
private List<Memo> memoList = new ArrayList<>();
// @OneToMany(mappedBy = "page")
// private List<Memo> memoList = new ArrayList<>();
}
22 changes: 22 additions & 0 deletions src/main/java/dev/gooiman/server/user/application/UserService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package dev.gooiman.server.user.application;

import dev.gooiman.server.common.exception.CommonException;
import dev.gooiman.server.common.exception.ErrorCode;
import dev.gooiman.server.user.repository.UserRepository;
import dev.gooiman.server.user.repository.entity.User;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class UserService {

private final UserRepository userRepository;

public User getUserByName(String name) {
return userRepository.findByName(name)
.orElseThrow(() -> new CommonException(ErrorCode.NOT_FOUND_USER));
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package dev.gooiman.server.user.repository;

import dev.gooiman.server.user.repository.entity.User;
import java.util.Optional;
import java.util.UUID;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends JpaRepository<User, UUID> {

@Query("select u from User u"
+ " where u.name=:name")
Optional<User> findByName(@Param("name") String name);
// Optional<User> findByPageAndName(String pageId, String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import java.util.UUID;
Expand All @@ -19,7 +20,7 @@
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Table(name = "USERS")
@Table(name = "users")
@Entity
public class User {

Expand All @@ -33,6 +34,6 @@ public class User {
private String password;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "PAGE_ID")
private Page page;

}

0 comments on commit 487da1d

Please sign in to comment.