Skip to content

Commit

Permalink
Merge pull request #77 from road-found-in-the-text/feature/#76/record…
Browse files Browse the repository at this point in the history
…_script

script record 저장
  • Loading branch information
ahyeon-github authored Feb 5, 2023
2 parents ad8c9fb + 3ddfc29 commit be4790f
Show file tree
Hide file tree
Showing 9 changed files with 258 additions and 0 deletions.
Binary file modified build/tmp/compileJava/previous-compilation-data.bin
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.example.umc3_teamproject.controller;
import com.example.umc3_teamproject.domain.dto.request.RecordScriptRequestDto;
import com.example.umc3_teamproject.domain.dto.request.ScriptRequestDto;
import com.example.umc3_teamproject.domain.dto.response.RecordScriptResponseDto;
import com.example.umc3_teamproject.domain.dto.response.ScriptResponseDto;
import com.example.umc3_teamproject.domain.item.RecordScript;
import com.example.umc3_teamproject.domain.item.Script;
import com.example.umc3_teamproject.repository.RecordRespository;
import com.example.umc3_teamproject.repository.ScriptRepository;
import com.example.umc3_teamproject.service.RecordScriptService;
import com.example.umc3_teamproject.service.ScriptService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@Slf4j
@RestController // Json 형태로 객체 데이터를 반환 (@Controller + @ResponseBody)
@RequestMapping("/script")
@RequiredArgsConstructor

public class RecordController {

private final RecordScriptService recordscriptService;
private final RecordRespository recordscriptRepository;

private final RecordScriptResponseDto recordscriptResponseDto;


@PostMapping("/addRecord")
public ResponseEntity<?> recordScript(@RequestBody RecordScriptRequestDto.Register record_script ){

return recordscriptService.recordScript(record_script);
}



@GetMapping("/record/{id}")
public ResponseEntity<?> readRecordScriptById(@PathVariable("id") Long id) {


Optional<RecordScript> optionalProduct=recordscriptRepository.findById(id);
if (optionalProduct.isPresent()) {
RecordScript record_script1 = optionalProduct.get();
return recordscriptResponseDto.success(record_script1);
}
return null;
}

}
6 changes: 6 additions & 0 deletions src/main/java/com/example/umc3_teamproject/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.example.umc3_teamproject.config.BaseTimeEntity;
import com.example.umc3_teamproject.domain.item.Comment;
import com.example.umc3_teamproject.domain.item.Forum;
import com.example.umc3_teamproject.domain.item.RecordScript;
import com.example.umc3_teamproject.domain.item.Script;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
Expand Down Expand Up @@ -68,6 +69,11 @@ public class Member extends BaseTimeEntity {
@JsonBackReference
private List<Forum> forums = new ArrayList<>();


@OneToMany(mappedBy = "memberId", fetch=FetchType.LAZY, cascade = CascadeType.ALL)
@JsonBackReference
private List<RecordScript> record_script;

@Builder
public Member(String socialId) {
this.socialId = socialId;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.example.umc3_teamproject.domain.dto.request;

import lombok.Getter;
import lombok.Setter;

import javax.validation.constraints.NotNull;

public class RecordScriptRequestDto {
@Getter
@Setter
public static class Register {

@NotNull(message = "user id는 필수 입력값입니다.")
private Long memberId;

@NotNull(message = "record는 필수 입력값입니다.")
private Float record;

@NotNull(message = "script id는 필수 입력값입니다.")
private Long scriptId;


}

@Getter
@Setter
public static class Update {

@NotNull(message = "record는 필수 입력값입니다.")
private Float record;


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.example.umc3_teamproject.domain.dto.response;

import com.example.umc3_teamproject.domain.item.RecordScript;
import com.example.umc3_teamproject.repository.MemberRepository;
import lombok.Builder;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;


@Component
public class RecordScriptResponseDto {
@Autowired
MemberRepository memberRepository;

@Getter
@Builder
private static class Body {

private String result;

private Long memberId;
private Long scriptId;

private Float record;

}

public ResponseEntity<?> success(RecordScript record_script) {

Body body = Body.builder()
.result("success")
.memberId(record_script.getMemberId().getId())
.scriptId(record_script.getScriptId().getScriptId())
.record(record_script.getRecord())
.build();
return ResponseEntity.ok(body);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.example.umc3_teamproject.domain.item;
import com.example.umc3_teamproject.domain.Member;
import com.example.umc3_teamproject.domain.item.Forum;
import com.example.umc3_teamproject.domain.item.Interview;
import com.example.umc3_teamproject.domain.item.Script;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import lombok.*;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import javax.validation.constraints.NotNull;

import static javax.persistence.FetchType.LAZY;
@Builder
@Entity
@Table(name="record_script")
@Where(clause = "deleted = false")
@JsonIdentityReference(alwaysAsId = true)
@AllArgsConstructor
@NoArgsConstructor
@Setter
@Getter
public class RecordScript {
@Id
@GeneratedValue(strategy = GenerationType.AUTO) // 식별자 값을 자동 생성
private Long id;


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

@ManyToOne(fetch = LAZY)
@JoinColumn(name = "script_id")
@JsonManagedReference
private Script scriptId;

@Column
private Float record;



}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public class Script extends BaseEntity {
@Column
private boolean deleted;


@OneToMany(mappedBy = "scriptId", fetch=FetchType.LAZY, cascade = CascadeType.ALL)
@JsonBackReference
private List<RecordScript> record_script;

@OneToMany(mappedBy = "scriptId", fetch=FetchType.LAZY, cascade = CascadeType.ALL)
@JsonBackReference
// private List<Paragraph> paragraphList = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.umc3_teamproject.repository;
import com.example.umc3_teamproject.domain.item.RecordScript;
import com.example.umc3_teamproject.domain.item.Script;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface RecordRespository extends JpaRepository<RecordScript, Long>, CrudRepository<RecordScript, Long> {
Optional<RecordScript> findById(Long id);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.example.umc3_teamproject.service;


import com.example.umc3_teamproject.domain.Member;
import com.example.umc3_teamproject.domain.dto.request.RecordScriptRequestDto;
import com.example.umc3_teamproject.domain.dto.request.ScriptRequestDto;
import com.example.umc3_teamproject.domain.dto.response.RecordScriptResponseDto;
import com.example.umc3_teamproject.domain.dto.response.ScriptResponseDto;
import com.example.umc3_teamproject.domain.item.Paragraph;
import com.example.umc3_teamproject.domain.item.RecordScript;
import com.example.umc3_teamproject.domain.item.Script;
import com.example.umc3_teamproject.repository.MemberRepository;
import com.example.umc3_teamproject.repository.RecordRespository;
import com.example.umc3_teamproject.repository.ScriptRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.transaction.Transactional;
import java.util.List;

@Service
@RequiredArgsConstructor
public class RecordScriptService {
@Autowired
private final MemberRepository memberRepository;
@Autowired
private final ScriptRepository scriptRepository;
@Autowired
private final RecordRespository recordscriptRepository;
private final RecordScriptResponseDto recordscriptResponse;


public ResponseEntity<?> recordScript(RecordScriptRequestDto.Register record_script1) {

Member script_member = memberRepository.getUser(record_script1.getMemberId());
Script record1_script = scriptRepository.getById(record_script1.getScriptId());


RecordScript record_script= RecordScript.builder()
.memberId(script_member)
.scriptId(record1_script)
.record(record_script1.getRecord())
.build();
recordscriptRepository.save(record_script);
return recordscriptResponse.success(record_script);
}
@Transactional
public void saveItem(RecordScript recordScript) {

recordscriptRepository.save(recordScript);


}



}

0 comments on commit be4790f

Please sign in to comment.