Skip to content

[고희준] 1주차 과제 #14

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
41 changes: 41 additions & 0 deletions src/main/java/doit/jpastudy2/repository/Attendant.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package doit.jpastudy2.repository;


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

@Entity
@NoArgsConstructor
@Getter
public class Attendant {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)

@Column(name = "n주차")
private int id;
Comment on lines +14 to +18
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

우선 자동으로 할당해주신 점 좋습니다!
n주차라고 작성해주신 것 보니, 아마 1주차에 해당하는 출석들을 모아서 설정하시려는 것 같은데 맞을까요?
맞다면 요 로직보다는 다음과 같이 작성해주시는 편이 좋을 것 같습니다

Suggested change
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "n주차")
private int id;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "attendant_id")
private int id;
@Column(name = "attendant_week_number")
private int weekNumber;

pk에는 의미가 없는 인공적인 값을 넣어주시는 것이 DB 구조 상 로직 작성하기에 편하실 것 같습니다! 참고하시면 될 것 같아요


@Column(name = "이름")
private String name;

@Column(name = "출석")
private String attendance;

@Column(name = "지각")
private String late;

@Column(name = "결석")
private String absence;

@Builder
public Attendant(String name, String attendance, String late, String absence) {
this.name = name;
this.attendance = attendance;
this.late = late;
this.absence = absence;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package doit.jpastudy2.repository;

import org.springframework.data.jpa.repository.JpaRepository;

public interface AttendantRepository extends JpaRepository<Attendant, Long> {
Attendant findByName(String name);
}
41 changes: 41 additions & 0 deletions src/main/java/doit/jpastudy2/repository/Student.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package doit.jpastudy2.repository;


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

@Entity
@NoArgsConstructor
@Getter
public class Student {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)


@Column(name = "학번")
private int id;

@Column(name = "이름")
private String name;

@Column(name = "학년")
private int grade;

@Column(name = "학과")
private String major;

@Column(name = "메일")
private String email;

@Builder
public Student(String name, int grade, String major, String email) {
this.name = name;
this.grade = grade;
this.major = major;
this.email = email;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package doit.jpastudy2.repository;

import org.springframework.data.jpa.repository.JpaRepository;

public interface StudentRepository extends JpaRepository<Student, Long> {
Student findByEmail(String email);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package doit.jpastudy2.repository;

import jakarta.transaction.Transactional;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@Transactional
@SpringBootTest
class AttendantRepositoryTest {

@Autowired
private AttendantRepository attendantRepository;

@DisplayName("save 테스트")
@Test
void test(){
Attendant atd1 = Attendant.builder()
.attendance("O")
.build();

Attendant atd2 = Attendant.builder()
.absence("O")
.build();

attendantRepository.save(atd1);
attendantRepository.save(atd2);

List<Attendant> attendants = attendantRepository.findAll();
Assertions.assertThat(attendants).hasSize(2);
Assertions.assertThat(attendants.get(0).getAttendance()).isEqualTo("O");
}

@Test
void findByName() {
Attendant atd1 = Attendant.builder()
.name("고희준")
.build();

Attendant atd2 = Attendant.builder()
.name("준희고")
.build();

attendantRepository.saveAll(List.of(atd1, atd2));

Attendant result1 = attendantRepository.findByName("희준고");
Attendant result2 = attendantRepository.findByName("준희고");
Attendant result3 = attendantRepository.findByName("준희고");

Assertions.assertThat(result1).isNull();
Assertions.assertThat(result2).isNotNull();
Assertions.assertThat(result3.getName()).isEqualTo("준희고");



}
}
54 changes: 54 additions & 0 deletions src/test/java/doit/jpastudy2/repository/StudentRepositoryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package doit.jpastudy2.repository;

import jakarta.transaction.Transactional;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@Transactional
@SpringBootTest
class StudentRepositoryTest {

@Autowired
private StudentRepository studentRepository;

@Test
void test(){
Student student1 = Student.builder()
.grade(4)
.major("사이버보안학과")
.build();

studentRepository.save(student1);

List<Student> students = studentRepository.findAll();
Assertions.assertThat(students).hasSize(1);
Assertions.assertThat(students.get(0).getGrade()).isEqualTo(4);
}
@Test
void findByEmail() {
Student student1 = Student.builder()
.grade(4)
.email("[email protected]")
.build();

Student student2 = Student.builder()
.grade(3)
.email("[email protected]")
.build();

studentRepository.save(student1);
studentRepository.save(student2);

Student result1 = studentRepository.findByEmail("[email protected]");
Student result2 = studentRepository.findByEmail("[email protected]");

Assertions.assertThat(result1).isNotNull();
Assertions.assertThat(result2).isNotNull();
Assertions.assertThat(result2).isNotEqualTo("[email protected]");

}
}