Skip to content

Commit

Permalink
[#4] 세번째 세미나 코드
Browse files Browse the repository at this point in the history
  • Loading branch information
mmihye committed Nov 10, 2023
1 parent ba66abc commit 34a563d
Show file tree
Hide file tree
Showing 47 changed files with 430 additions and 84 deletions.
Binary file modified Week1/seminar/.gradle/8.2.1/checksums/checksums.lock
Binary file not shown.
Binary file modified Week1/seminar/.gradle/8.2.1/checksums/md5-checksums.bin
Binary file not shown.
Binary file modified Week1/seminar/.gradle/8.2.1/checksums/sha1-checksums.bin
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified Week1/seminar/.gradle/8.2.1/fileHashes/fileHashes.bin
Binary file not shown.
Binary file modified Week1/seminar/.gradle/8.2.1/fileHashes/fileHashes.lock
Binary file not shown.
Binary file modified Week1/seminar/.gradle/8.2.1/fileHashes/resourceHashesCache.bin
Binary file not shown.
Binary file modified Week1/seminar/.gradle/buildOutputCleanup/buildOutputCleanup.lock
Binary file not shown.
Binary file modified Week1/seminar/.gradle/file-system.probe
Binary file not shown.
105 changes: 82 additions & 23 deletions Week1/seminar/.idea/workspace.xml

Large diffs are not rendered by default.

23 changes: 17 additions & 6 deletions Week1/seminar/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,28 @@ configurations {
repositories {
mavenCentral()
}

dependencies {
//jpa
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'

// H2 Datbase 추가
runtimeOnly 'com.h2database:h2'

// spring boot
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'

// lombok
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'


// h2
runtimeOnly 'com.h2database:h2'

// mysql
runtimeOnly 'com.mysql:mysql-connector-j'

// security
implementation 'org.springframework.boot:spring-boot-starter-security'
testImplementation 'org.springframework.security:spring-security-test'

// test
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
16 changes: 0 additions & 16 deletions Week1/seminar/build/resources/main/application.yml

This file was deleted.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
//@EnableJpaAuditing
public class ServerSeminarApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.server.dosopt.seminar.config;

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

@Configuration
@EnableJpaAuditing
public class JpaAuditingConfig {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.server.dosopt.seminar.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@EnableWebSecurity
public class SecurityConfig {


@Bean
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
return http.csrf().disable()
.authorizeHttpRequests()
.anyRequest().permitAll()
.and().build();
}

@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedOriginPatterns("*")
.allowedMethods("*");
}
};
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.server.dosopt.seminar.controller;

import com.server.dosopt.seminar.dto.request.MemberCreateRequest;
import com.server.dosopt.seminar.dto.request.member.MemberCreateRequest;
import com.server.dosopt.seminar.dto.response.MemberGetResponse;
import com.server.dosopt.seminar.service.MemberService;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.coyote.Response;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

Expand All @@ -20,18 +18,18 @@ public class MemberController {
private final MemberService memberService;

@GetMapping("{memberId}")
public ResponseEntity<MemberGetResponse> getMemberProfileV1(@PathVariable("memberId") Long memberId){
public ResponseEntity<MemberGetResponse> getMemberProfileV1(@PathVariable("memberId") Long memberId) {
MemberGetResponse response = memberService.getByIdV1(memberId);
return ResponseEntity.ok(response);
}

@GetMapping(value = "{memberId}/v2", produces = "application/json")
public ResponseEntity<MemberGetResponse> getMemberProfileV2(@PathVariable Long memberId){
public ResponseEntity<MemberGetResponse> getMemberProfileV2(@PathVariable Long memberId) {
return ResponseEntity.ok(memberService.getByIdV2(memberId));
}

@GetMapping
public ResponseEntity<List<MemberGetResponse>> getMembersProfile(){
public ResponseEntity<List<MemberGetResponse>> getMembersProfile() {
return ResponseEntity.ok(memberService.getMembers());
}

Expand All @@ -41,5 +39,4 @@ public ResponseEntity<Void> createMember(@RequestBody MemberCreateRequest reques
return ResponseEntity.created(location).build();

}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.server.dosopt.seminar.controller;

import com.server.dosopt.seminar.dto.request.post.PostCreateRequest;
import com.server.dosopt.seminar.dto.response.PostGetResponse;
import com.server.dosopt.seminar.service.PostService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.util.List;

@RestController
@RequestMapping("/api/posts")
@RequiredArgsConstructor
public class PostController {
private static final String CUSTOM_AUTH_ID = "X-Auth-Id";
private final PostService postService;

@PostMapping
public ResponseEntity<Void> createPost(@RequestHeader(CUSTOM_AUTH_ID) Long memberId,
@RequestBody PostCreateRequest request){

URI location = URI.create("/api/post/" + postService.create(request, memberId));
return ResponseEntity.created(location).build();
}

@GetMapping("{postId}")
public ResponseEntity<PostGetResponse> getPostById(@PathVariable Long postId) {
return ResponseEntity.ok(postService.getById(postId));
}

@GetMapping
public ResponseEntity<List<PostGetResponse>> getPosts(@RequestHeader(CUSTOM_AUTH_ID) Long memberId) {
return ResponseEntity.ok(postService.getPosts(memberId));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.server.dosopt.seminar.domain;


import jakarta.persistence.EntityListeners;
import jakarta.persistence.MappedSuperclass;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;

@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class BaseTimeEntity {

@CreatedDate // 현재시각으로 초기화해줌
private LocalDateTime createdAt;

@LastModifiedDate
private LocalDateTime updateAt;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Member {
public class Member extends BaseTimeEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String nickname;
private int age;
private int age; // age에 int 사용. short를 사용해도 좋을지도?

@Embedded
private SOPT sopt;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.server.dosopt.seminar.domain;

import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@Table(name = "post")
public class Post extends BaseTimeEntity{

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

private String title;

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

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
private Member member;

@Builder
public Post(String title, String content, Member member) {
this.title = title;
this.content = content;
this.member = member;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.server.dosopt.seminar.dto.request;
package com.server.dosopt.seminar.dto.request.member;

import com.server.dosopt.seminar.domain.SOPT;
import lombok.Data;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.server.dosopt.seminar.dto.request.post;

public record PostCreateRequest(
String title,
String content
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@

import lombok.Getter;

@Getter
public class HealthCheckResponse {
private static final String STATUS = "OK";

private String status;
//@Getter
//public class HealthCheckResponse {
// private static final String STATUS = "OK";
//
// private String status;
//
// public HealthCheckResponse(){
// this.status = STATUS;
// }
//}

public record HealthCheckResponse() {
public HealthCheckResponse(){
this.status = STATUS;

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.server.dosopt.seminar.dto.response;

import com.server.dosopt.seminar.domain.Post;

public record PostGetResponse(
Long id,
String title,
String content
) {
public static PostGetResponse of(Post post) {
return new PostGetResponse(
post.getId(),
post.getTitle(),
post.getContent()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.server.dosopt.seminar.exception;

public class BadRequestException extends RuntimeException{
public BadRequestException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.server.dosopt.seminar.exception;

public class BusinessException extends RuntimeException{
public BusinessException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.server.dosopt.seminar.exception;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(IllegalArgumentException.class)
public ResponseEntity<Void> handleIllegalArgumentException(final IllegalArgumentException e) {
return ResponseEntity.badRequest().build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.server.dosopt.seminar.reposiotry;

import com.server.dosopt.seminar.domain.Member;
import com.server.dosopt.seminar.domain.Post;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface PostJpaRepository extends JpaRepository<Post,Long> {

List<Post> findAllByMemberId(Long memberId);
List<Post> findAllByMember(Member member);
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
package com.server.dosopt.seminar.service;

import com.server.dosopt.seminar.domain.Member;
import com.server.dosopt.seminar.dto.request.MemberCreateRequest;
import com.server.dosopt.seminar.dto.request.member.MemberCreateRequest;
import com.server.dosopt.seminar.dto.response.MemberGetResponse;
import com.server.dosopt.seminar.reposiotry.MemberJpaRepository;
import jakarta.persistence.EntityNotFoundException;
import lombok.RequiredArgsConstructor;
import org.apache.coyote.Response;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.net.URI;
import java.util.List;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -44,6 +39,7 @@ public List<MemberGetResponse> getMembers(){

}


// 메소드에 붙인 Transactional이 우선 적용
@Transactional
public String create(MemberCreateRequest request){
Expand Down
Loading

0 comments on commit 34a563d

Please sign in to comment.