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

Order #44

Open
wants to merge 8 commits into
base: master
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 @@ -8,5 +8,8 @@

public interface CommentRepo extends JpaRepository<Comment, Long> {
@Query("SELECT c FROM Comment c WHERE c.postId = :postId ORDER BY CASE WHEN c.userId = :userId THEN 0 ELSE 1 END, c.likeCount DESC, c.createdAt ASC")
Page<Comment> findByPostId(Long postId, Long userId, Pageable pageable);
Page<Comment> findByPostIdOrderByLikeDesc(Long postId, Long userId, Pageable pageable);

@Query("SELECT c FROM Comment c WHERE c.postId = :postId ORDER BY CASE WHEN c.userId = :userId THEN 0 ELSE 1 END, c.createdAt ASC")
Page<Comment> findByPostOrderByCreatedAtDesc(Long postId, Long userId, Pageable pageable);
Copy link
Member

@qlido qlido Oct 2, 2023

Choose a reason for hiding this comment

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

Pageable에 OrderBy를 사용하지 않은 이유가 있나요??

}
5 changes: 5 additions & 0 deletions src/main/java/com/insert/ogbsm/domain/common/OrderType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.insert.ogbsm.domain.common;

public enum OrderType {
RECENT, LIKE
}
8 changes: 3 additions & 5 deletions src/main/java/com/insert/ogbsm/domain/post/repo/PostRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

public interface PostRepo extends JpaRepository<Post, Long> {

@Query("select p from Post p where p.category = :category " +
"order by p.likeCount desc, " +
"p.createdAt desc ")
Page<Post> findByCategory(Category category, Pageable pageable);
Page<Post> findByCategoryOrderByLikeCountDescCreatedAtDesc(Category category, Pageable pageable);

Page<Post> findByCategoryOrderByCreatedAtDesc(Category category, Pageable pageable);
}
264 changes: 264 additions & 0 deletions src/main/java/com/insert/ogbsm/domain/timeTable/Period.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
package com.insert.ogbsm.domain.timeTable;

import com.insert.ogbsm.infra.error.exception.BsmException;
import com.insert.ogbsm.infra.error.exception.ErrorCode;
import com.insert.ogbsm.presentation.timeTable.dto.TimeTableValueRes;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.time.DayOfWeek;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

import static java.time.DayOfWeek.*;

@AllArgsConstructor
@Getter
public enum Period {
// 기숙사
MORNING_DORM(new Time(0, 0), new Time(6, 30)),
MORNING_DORM_FRIDAY(new Time(0, 0), new Time(7, 30)),
PREPARING_DORM(new Time(6, 50), new Time(7, 30)),
NIGHT_DORM(new Time(20, 40), new Time(24, 0)),

// 아침운동
MORNING_EXERSIZE(new Time(6, 30), new Time(6, 50)),

//아침 자습시간
MORNING_SELF_STUDY(new Time(8, 5), new Time(8, 30)),

//시간표
FIRST(new Time(8, 40), new Time(9, 30)),
SECOND(new Time(9, 40), new Time(10, 30)),
THIRD(new Time(10, 40), new Time(11, 30)),
FOURTH(new Time(11, 40), new Time(12, 30)),
FIFTH(new Time(1, 20), new Time(2, 10)),
SIXTH(new Time(2, 20), new Time(3, 10)),
SEVENTH(new Time(3, 20), new Time(4, 10)),
AFTER_SCHOOL_1(new Time(4, 30), new Time(6, 10)),
AFTER_SCHOOL_2(new Time(7, 0), new Time(8, 40)),

//조례, 종례
PRONUNCIATION(new Time(8, 30), new Time(8, 40)),
ANTONYM(new Time(4, 20), new Time(4, 30)),
CLEANING(new Time(4, 10), new Time(4, 20)),

//쉬는 시간
BREAK_1(new Time(9, 30), new Time(9, 40)),
BREAK_2(new Time(10, 30), new Time(10, 40)),
BREAK_3(new Time(11, 30), new Time(11, 40)),
BREAK_4(new Time(2, 10), new Time(2, 20)),
BREAK_5(new Time(3, 10), new Time(3, 20)),

//식사
BREAKFAST(new Time(7, 30), new Time(8, 5)),
LUNCH(new Time(12, 30), new Time(1, 20)),
DINNER(new Time(6, 10), new Time(7, 0)),

//금요일
ANTONYM_FRIDAY(new Time(3, 10), new Time(3, 20)),
GO_HOME(new Time(3, 20), new Time(24, 0)),

//토요일
HOME(new Time(0, 0), new Time(24, 0)),

//일요일
HOME_SUNDAY(new Time(0, 0), new Time(7, 50)),
DORM_SUNDAY(new Time(7, 50), new Time(24, 0));


final Time startTime;
final Time endTime;

public static Period get(int period) {
switch (period) {
case 1:
return FIRST;
case 2:
return SECOND;
case 3:
return THIRD;
case 4:
return FOURTH;
case 5:
return FIFTH;
case 6:
return SIXTH;
case 7:
return SEVENTH;
}
throw new BsmException(ErrorCode.NO_PERIOD_MATCHED);
}

public static Map<DayOfWeek, List<TimeTableValueRes>> getAllPeriod(Map<DayOfWeek, List<TimeTableValueRes>> periodMap) {
setAtLeastSeventhAndBreak(periodMap);

for (DayOfWeek day : periodMap.keySet()) {
switch (day) {
case MONDAY -> {
List<TimeTableValueRes> monday = getMonday(periodMap.get(day));
periodMap.replace(day, monday);
}
case TUESDAY -> {
List<TimeTableValueRes> tuesday = getTuesday(periodMap.get(day));
periodMap.replace(day, tuesday);
}
case WEDNESDAY -> {
List<TimeTableValueRes> wednesday = getWednesday(periodMap.get(day));
periodMap.replace(day, wednesday);
}
case THURSDAY -> {
List<TimeTableValueRes> thursday = getThursday(periodMap.get(day));
periodMap.replace(day, thursday);
}
case FRIDAY -> {
List<TimeTableValueRes> friday = getFriday(periodMap.get(day));
periodMap.replace(day, friday);
}
}
}
List<TimeTableValueRes> saturday = getSaturday();
List<TimeTableValueRes> sunday = getSunday();

periodMap.put(SATURDAY, saturday);
periodMap.put(SUNDAY, sunday);

return periodMap;
}

private static void setAtLeastSeventhAndBreak(Map<DayOfWeek, List<TimeTableValueRes>> periodMap) {
for (DayOfWeek day : periodMap.keySet()) {
if (day != FRIDAY) {
List<TimeTableValueRes> timeTableValueRes = periodMap.get(day);
for (int i = periodMap.get(day).size(); i < 7; i++) {
timeTableValueRes.add(
new TimeTableValueRes(
get(i + 1),
"자습",
null
)
);
}
periodMap.replace(day, setBreakAndLunch(timeTableValueRes));
}
}
}

public static Map<DayOfWeek, List<TimeTableValueRes>> setAtLeastSeventh(Map<DayOfWeek, List<TimeTableValueRes>> periodMap) {
for (DayOfWeek day : periodMap.keySet()) {
List<TimeTableValueRes> timeTableValueRes = periodMap.get(day);
for (int i = periodMap.get(day).size(); i < 7; i++) {
timeTableValueRes.add(
new TimeTableValueRes(
get(i + 1),
"자습",
null
)
);
}
}

return periodMap;
}

private static List<TimeTableValueRes> getMonday(List<TimeTableValueRes> timeTableValueRes) {
List<TimeTableValueRes> monday = setOrdinaryMorning(timeTableValueRes);
return setOrdinaryAfternoon(monday);

}

private static List<TimeTableValueRes> getTuesday(List<TimeTableValueRes> timeTableValueRes) {
List<TimeTableValueRes> tuesday = setOrdinaryMorning(timeTableValueRes);
return setOrdinaryAfternoon(tuesday);
}

private static List<TimeTableValueRes> getWednesday(List<TimeTableValueRes> timeTableValueRes) {
List<TimeTableValueRes> wednesday = setOrdinaryMorning(timeTableValueRes);
return setOrdinaryAfternoon(wednesday);
}

private static List<TimeTableValueRes> getThursday(List<TimeTableValueRes> timeTableValueRes) {
List<TimeTableValueRes> thursday = setOrdinaryMorning(timeTableValueRes);
return setOrdinaryAfternoon(thursday);
}

private static List<TimeTableValueRes> setBreakAndLunch(List<TimeTableValueRes> timeTableValueRes) {
timeTableValueRes.add(1, new TimeTableValueRes(BREAK_1, "쉬는 시간", null));
timeTableValueRes.add(3, new TimeTableValueRes(BREAK_2, "쉬는 시간", null));
timeTableValueRes.add(5, new TimeTableValueRes(BREAK_3, "쉬는 시간", null));
timeTableValueRes.add(7, new TimeTableValueRes(LUNCH, "점심 시간", null));
timeTableValueRes.add(9, new TimeTableValueRes(BREAK_4, "쉬는 시간", null));
timeTableValueRes.add(11, new TimeTableValueRes(BREAK_5, "쉬는 시간", null));

return timeTableValueRes;
}

private static List<TimeTableValueRes> setOrdinaryMorning(List<TimeTableValueRes> day) {
List<TimeTableValueRes> ordinary = new java.util.ArrayList<>(Stream.of(
new TimeTableValueRes(MORNING_DORM, "기숙사", null),
new TimeTableValueRes(MORNING_EXERSIZE, "아침 운동", null),
new TimeTableValueRes(PREPARING_DORM, "등교 준비", null),
new TimeTableValueRes(BREAKFAST, "아침 식사", null),
new TimeTableValueRes(MORNING_SELF_STUDY, "아침 자습 시간", null),
new TimeTableValueRes(PRONUNCIATION, "조례", null)
).toList());

ordinary.addAll(day);

return ordinary;
}

private static List<TimeTableValueRes> setOrdinaryAfternoon(List<TimeTableValueRes> day) {
List<TimeTableValueRes> ordinary = Stream.of(
new TimeTableValueRes(CLEANING, "청소 시간", null),
new TimeTableValueRes(ANTONYM, "종례", null),
new TimeTableValueRes(AFTER_SCHOOL_1, "방과후", null),
new TimeTableValueRes(DINNER, "저녁 식사", null),
new TimeTableValueRes(AFTER_SCHOOL_2, "방과후", null),
new TimeTableValueRes(NIGHT_DORM, "기숙사", null)
).toList();

day.addAll(ordinary);

return day;
}

private static List<TimeTableValueRes> getFriday(List<TimeTableValueRes> timeTableValueRes) {
List<TimeTableValueRes> friday = new java.util.ArrayList<>(Stream.of(
new TimeTableValueRes(MORNING_DORM_FRIDAY, "기숙사", null),
new TimeTableValueRes(BREAKFAST, "아침 식사", null),
new TimeTableValueRes(MORNING_SELF_STUDY, "아침 자습 시간", null),
new TimeTableValueRes(PRONUNCIATION, "조례", null)
).toList());

friday.addAll(timeTableValueRes);

friday.addAll(
List.of(
new TimeTableValueRes(ANTONYM_FRIDAY, "종례", null),
new TimeTableValueRes(GO_HOME, "집", null)
)
);

return friday;
}

private static List<TimeTableValueRes> getSaturday() {
return List.of(
new TimeTableValueRes(HOME, "집", null)
);
}

private static List<TimeTableValueRes> getSunday() {
return List.of(
new TimeTableValueRes(HOME_SUNDAY, "집", null),
new TimeTableValueRes(DORM_SUNDAY, "기숙사", null)
);
}

public record Time(int hour, int minute) {

}

}
24 changes: 24 additions & 0 deletions src/main/java/com/insert/ogbsm/domain/timeTable/TimeTable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.insert.ogbsm.domain.timeTable;

import jakarta.persistence.EmbeddedId;
import jakarta.persistence.Entity;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.time.LocalDate;

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class TimeTable {

@EmbeddedId
private TimeTablePk pk;

private String subject;

public TimeTable(LocalDate date, int grade, int classNumber, Period period, String subject) {
this.pk = new TimeTablePk(date, grade, classNumber, period);
this.subject = subject;
}

}
35 changes: 35 additions & 0 deletions src/main/java/com/insert/ogbsm/domain/timeTable/TimeTablePk.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.insert.ogbsm.domain.timeTable;

import jakarta.persistence.Embeddable;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.time.LocalDate;

@Getter
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@Embeddable
@NoArgsConstructor
public class TimeTablePk implements Serializable {

@EqualsAndHashCode.Include
private LocalDate date;

@EqualsAndHashCode.Include
private int grade;

@EqualsAndHashCode.Include
private int classNumber;

@EqualsAndHashCode.Include
private Period period;

public TimeTablePk(LocalDate date, int grade, int classNumber, Period period) {
this.date = date;
this.grade = grade;
this.classNumber = classNumber;
this.period = period;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.insert.ogbsm.domain.timeTable.repo;

import com.insert.ogbsm.presentation.timeTable.dto.TimeTableValueRes;

import java.time.LocalDate;
import java.util.List;

public interface TimeTableDao {
List<TimeTableValueRes> findByGradeAndClassNumber(int grade, int classNumber, List<LocalDate> startAndEndOfWeek);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.insert.ogbsm.domain.timeTable.repo;

import com.insert.ogbsm.presentation.timeTable.dto.QTimeTableValueRes;
import com.insert.ogbsm.presentation.timeTable.dto.TimeTableValueRes;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;

import java.time.LocalDate;
import java.util.List;

import static com.insert.ogbsm.domain.timeTable.QTimeTable.timeTable;

@RequiredArgsConstructor
public class TimeTableDaoImpl implements TimeTableDao {
private final JPAQueryFactory jpaQueryFactory;

@Override
public List<TimeTableValueRes> findByGradeAndClassNumber(int grade, int classNumber, List<LocalDate> startAndEndOfWeek) {
return jpaQueryFactory
.select(new QTimeTableValueRes(timeTable.pk.period, timeTable.subject, timeTable.pk.date))
.from(timeTable)
.where(timeTable.pk.grade.eq(grade)
.and(timeTable.pk.classNumber.eq(classNumber)))
.where(timeTable.pk.date.between(startAndEndOfWeek.get(0), startAndEndOfWeek.get(1)))
.orderBy(timeTable.pk.date.asc(), timeTable.pk.period.asc())
.fetch();
}
}
Loading