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

3단계 - 수강신청(DB 적용) #515

Open
wants to merge 5 commits into
base: movingone
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@

public interface CourseRepository {
int save(Course course);

Course findById(Long id);
}
62 changes: 0 additions & 62 deletions src/main/java/nextstep/courses/domain/Image.java

This file was deleted.

1 change: 1 addition & 0 deletions src/main/java/nextstep/courses/domain/Session.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package nextstep.courses.domain;

import nextstep.image.domain.Image;
import nextstep.payments.domain.Payment;

import java.time.LocalDateTime;
Expand Down
16 changes: 3 additions & 13 deletions src/main/java/nextstep/courses/domain/SessionStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,12 @@

public class SessionStatus {

private String status;
private final String prepare = "prepare";
private final String end = "end";
private final String recruit = "recruit";

private Status status;
public SessionStatus(String status) {
statusCheck(status);
this.status = status;
}

private void statusCheck(String status) {
if (!(status.equals(prepare) || status.equals(end) || status.equals(recruit)))
throw new IllegalArgumentException("잘못된 강의 상태 입니다");
this.status = Status.fromString(status);
}

public boolean check() {
return status.equals(recruit);
return this.status == Status.RECRUIT;
}
}
25 changes: 25 additions & 0 deletions src/main/java/nextstep/courses/domain/Status.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package nextstep.courses.domain;

public enum Status {

PREPARE("prepare"),
END("end"),
RECRUIT("recruit");

private final String status;
Status(String status) {
this.status = status;
}

public String getStatus() {
return status;
}

public static Status fromString(String status) {
for (Status state : Status.values()) {
if (state.getStatus().equals(status))
return state;
}
throw new IllegalArgumentException(status + "는 잘못된 강의 상태 입니다");
}
}
5 changes: 0 additions & 5 deletions src/main/java/nextstep/courses/domain/Type.java

This file was deleted.

138 changes: 138 additions & 0 deletions src/main/java/nextstep/image/domain/Image.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package nextstep.image.domain;

import java.util.Objects;

public class Image {

private Long id;
private Long sessionId;
private long size;
private final int MB = 1024 * 1024;
private final int MINIMUM_WIDTH = 300;
private final int MINIMUM_HEIGHT = 200;
private ImageType imageType;
private int width;
private int height;
private String filePath;
private String fileName;
private String extension;


public Image(int size, ImageType type, int width, int height) {

this.size = size;
this.imageType = type;
this.width = width;
this.height = height;
}

public Image(Long sessionId, int width, int height, long size, String fileName, String extension, String filePath) {
this(0L, sessionId, width, height, size, fileName, extension, filePath);
}

public Image(Long id, Long sessionId, int width, int height, long size, String fileName, String extension, String filePath) {
if (sessionId == null)
throw new IllegalArgumentException("강의 번호가 잘못되었습니다");
imagePixel(width, height);
imageSize(size);
imageType(fileName);
this.id = id;
this.sessionId = sessionId;
this.width = width;
this.height = height;
this.size = size;
this.fileName = fileName;
this.filePath = filePath;
this.extension = extension;
}

private void imageSize(long size) {
if (size > 1 * MB)
throw new IllegalArgumentException("이미지 파일 크기가 너무 큽니다");
}
private void imagePixel(int width, int height) {
if (width < MINIMUM_WIDTH || height < MINIMUM_HEIGHT)
throw new IllegalArgumentException("크기가 맞지 않습니다");
if (!(width * 2 == height * 3))
throw new IllegalArgumentException("비율이 맞지 않습니다");
}

private void imageType(String type) {
for (ImageType value : ImageType.values()) {
if (!value.name().equals(type))
throw new IllegalArgumentException("잘못된 파일 타입입니다");
}
}

public long getSize() {
return size;
}

public ImageType getImageType() {
return imageType;
}

public int getWidth() {
return width;
}

public int getHeight() {
return height;
}

public int getMB() {
return MB;
}

public int getMINIMUM_WIDTH() {
return MINIMUM_WIDTH;
}

public int getMINIMUM_HEIGHT() {
return MINIMUM_HEIGHT;
}

public Long getId() {
return id;
}

public Long getSessionId() {
return sessionId;
}

public String getFilePath() {
return filePath;
}

public String getFileName() {
return fileName;
}

public String getExtension() {
return extension;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Image image = (Image) o;
return size == image.size && width == image.width && height == image.height && imageType == image.imageType;
}

@Override
public int hashCode() {
return Objects.hash(size, MB, imageType, width, height);
}

@Override
public String toString() {
return "Image{" +
"size=" + size +
", MB=" + MB +
", type=" + imageType +
", width=" + width +
", height=" + height +
'}';
}
}
16 changes: 16 additions & 0 deletions src/main/java/nextstep/image/domain/ImageRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package nextstep.image.domain;

import nextstep.image.domain.Image;

import java.util.List;

public interface ImageRepository {

int save(Image image);

Image findById(Long id);

List<Image> findBySessionId(Long id);

void saveAll(List<Image> images);
}
16 changes: 16 additions & 0 deletions src/main/java/nextstep/image/domain/ImageType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package nextstep.image.domain;

public enum ImageType {

GIT("git"),
JPG("jpg"),
JPEG("jpeg"),
PNG("png"),
SVG("svg");

String type;

ImageType(String type) {
this.type = type;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package nextstep.image.infrastructure;

import nextstep.image.domain.Image;
import nextstep.image.domain.ImageRepository;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.ParameterizedPreparedStatementSetter;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("ImageRepository")

public class JdbcImageRepository implements ImageRepository {

private final JdbcOperations jdbcTempleate;

public JdbcImageRepository(JdbcOperations jdbcTempleate) {
this.jdbcTempleate = jdbcTempleate;
}

@Override
public int save(Image image) {
String sql = "INSERT INTO nextstep.image(session_id, width, height, size, file_name, extension, file_path)\n" + "VALUES (?, ?, ?, ?, ?, ?, ?)";

return jdbcTempleate.update(sql, values(image));
}

private PreparedStatementSetter values(Image image) {
return ps -> {
ps.setLong(1, image.getSessionId());
ps.setInt(2, image.getWidth());
ps.setInt(3, image.getHeight());
ps.setLong(4, image.getSize());
ps.setString(5, image.getFileName());
ps.setString(6, image.getExtension());
ps.setString(7, image.getFilePath());
};
}

@Override
public Image findById(Long id) {
String sql = "SELECT id, session_id, width, height, size, file_name, extension, file_path from image where id = ?";
return jdbcTempleate.queryForObject(sql, imageRowMapper(), id);
}

private RowMapper<Image> imageRowMapper() {
return (rs, rowNum) -> new Image(
rs.getLong(1),
rs.getLong(2),
rs.getInt(3),
rs.getInt(4),
rs.getLong(5),
rs.getString(6),
rs.getString(7),
rs.getString(8)
);
}

@Override
public List<Image> findBySessionId(Long sessionId) {
String sql = "SELECT id, session_id, width, height, size, file_name, extension, file_path from nextstep.image where id = ?";
return jdbcTempleate.query(sql, imageRowMapper(), sessionId);
}

@Override
public void saveAll(List<Image> images) {
String sql = "INSERT INTO nextstep.image(session_id, width, height, size, file_name, extension, file_path)\n" + "VALUES (?, ?, ?, ?, ?, ?, ?)";
jdbcTempleate.batchUpdate(sql, images, images.size(), values());
}

private ParameterizedPreparedStatementSetter<Image> values() {
return (ps, image) -> {
ps.setLong(1, image.getSessionId());
ps.setInt(2, image.getWidth());
ps.setInt(3, image.getHeight());
ps.setLong(4, image.getSize());
ps.setString(5, image.getFileName());
ps.setString(6, image.getExtension());
ps.setString(7, image.getFilePath());
};
}
}
Loading