Skip to content

Commit

Permalink
Merge branch 'develop' into feat/28
Browse files Browse the repository at this point in the history
  • Loading branch information
tkdwns414 authored May 28, 2024
2 parents 0351c33 + f5a7f10 commit c8c9c3f
Show file tree
Hide file tree
Showing 29 changed files with 665 additions and 58 deletions.
39 changes: 35 additions & 4 deletions src/main/java/com/telepigeon/server/controller/RoomController.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package com.telepigeon.server.controller;

import com.telepigeon.server.annotation.UserId;
import com.telepigeon.server.domain.Profile;
import com.telepigeon.server.domain.Room;
import com.telepigeon.server.dto.room.request.RoomCreateDto;
import com.telepigeon.server.dto.room.request.RoomEnterDto;
import com.telepigeon.server.dto.room.response.RoomInfoDto;
import com.telepigeon.server.dto.room.response.RoomListDto;
import com.telepigeon.server.service.room.RoomService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.w3c.dom.stylesheets.LinkStyle;

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

@RestController
@RequiredArgsConstructor
Expand All @@ -29,4 +33,31 @@ public ResponseEntity<Void> createRoom(
URI location = URI.create("/rooms/" + createdRoom.getId());
return ResponseEntity.created(location).build();
}

@GetMapping("/rooms")
public ResponseEntity<RoomListDto> getAllRooms(@UserId Long userId) {
return ResponseEntity.ok(roomService.getAllRooms(userId));
}

@GetMapping("/rooms/{roomId}/info")
public ResponseEntity<RoomInfoDto> getRoomInfo(@PathVariable Long roomId) {
return ResponseEntity.ok(roomService.getRoomInfo(roomId));
}

@PostMapping("/rooms/entrance")
public ResponseEntity<Void> enterRoom(
@UserId Long userId,
@Valid @RequestBody RoomEnterDto roomEnterDto
) {
Profile createProfile = roomService.enterRoom(roomEnterDto, userId);
return ResponseEntity.created(URI.create("/profiles/" + createProfile.getId())).build();
}

@DeleteMapping("/rooms/{roomId}")
public ResponseEntity<Room> deleteRoom(
@UserId Long userId,
@PathVariable Long roomId
) {
return ResponseEntity.ok(roomService.deleteRoom(roomId, userId));
}
}
1 change: 1 addition & 0 deletions src/main/java/com/telepigeon/server/domain/Answer.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
@Table(name="answer")
@NoArgsConstructor(access=AccessLevel.PROTECTED)
public class Answer {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/com/telepigeon/server/domain/Profile.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ public class Profile {
private Room room;

@Builder
private Profile(Users user, Room room) {
private Profile(Users user, Room room, Relation relation) {
this.user = user;
this.room = room;
this.relation = relation;
}

public static Profile create(
Expand All @@ -62,4 +63,16 @@ public static Profile create(
.room(room)
.build();
}

public static Profile createTest(
Users user,
Room room,
Relation relation
) {
return Profile.builder()
.user(user)
.room(room)
.relation(relation)
.build();
}
}
17 changes: 17 additions & 0 deletions src/main/java/com/telepigeon/server/domain/Users.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

Expand All @@ -26,4 +27,20 @@ public class Users {

private LocalDateTime updatedAt;

@Builder
private Users(Long id, String name) {
this.id = id;
this.name = name;
}

public static Users create(
Long id,
String name
) {
return Users.builder()
.id(id)
.name(name)
.build();
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.telepigeon.server.dto.room.request;

import com.telepigeon.server.domain.Room;
import jakarta.annotation.Nullable;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.telepigeon.server.dto.room.request;

import jakarta.validation.constraints.NotNull;

public record RoomEnterDto(
@NotNull
String code
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.telepigeon.server.dto.room.response;

import com.telepigeon.server.domain.Room;

public record RoomInfoDto(
String code,
String name
) {
public static RoomInfoDto of(Room room) {
return new RoomInfoDto(
room.getCode(),
room.getName()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.telepigeon.server.dto.room.response;

import com.telepigeon.server.domain.Answer;
import com.telepigeon.server.domain.Profile;
import com.telepigeon.server.domain.Room;

import java.util.List;

public record RoomListDto(
List<RoomDto> rooms
) {

public static RoomListDto of(List<RoomDto> rooms) {
return new RoomListDto(rooms);
}

public record RoomDto(
Long roomId,
String name,
String opponentNickname,
String myRelation,
String opponentRelation,
Integer emotion,
Integer sentence
) {
public static RoomDto of(
Long roomId,
String name,
String opponentNickname,
String myRelation,
String opponentRelation,
Integer emotion,
Integer sentence
) {
return new RoomDto(roomId, name, opponentNickname, myRelation,opponentRelation, emotion, sentence);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
public enum NotFoundErrorCode implements DefaultErrorCode{
NOT_FOUND_END_POINT(HttpStatus.NOT_FOUND, "error", "존재하지 않는 API입니다."),
NOT_FOUND_HURRY(HttpStatus.NOT_FOUND, "error", "존재하지 않는 재촉하기 입니다."),
NOT_FOUND_FILE(HttpStatus.NOT_FOUND, "error", "존재하지 않는 파일 입니다."),
NOT_FOUND_FILE(HttpStatus.NOT_FOUND, "error", "존재하지 않는 파일입니다."),
ROOM_NOT_FOUND(HttpStatus.NOT_FOUND, "error", "존재하지 않는 방입니다."),
USER_NOT_FOUND(HttpStatus.NOT_FOUND, "error", "존재하지 않는 사용자입니다."),
QUESTION_NOT_FOUND(HttpStatus.NOT_FOUND, "error", "존재하지 않는 질문 입니다."),
PROFILE_NOT_FOUND(HttpStatus.NOT_FOUND, "error", "존재하지 않는 프로필입니다."),
WORRY_NOT_FOUND(HttpStatus.NOT_FOUND, "error", "존재하지 않는 걱정입니다"),
;

@JsonIgnore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ List<Answer> findAllByProfileInAndCreatedAtBetweenOrderByCreatedAt(
);
Optional<Answer> findFirstByProfileOrderByCreatedAtDesc(Profile profile);

List<Answer> findAllByProfile(Profile profile);

Boolean existsByQuestion(Question question);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import com.telepigeon.server.domain.Users;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;

public interface ProfileRepository extends JpaRepository<Profile, Long> {
Optional<Profile> findByUserAndRoom(Users user, Room room);
Optional<Profile> findByUserNotAndRoom(Users user, Room room);
List<Profile> findAllByUserId(Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
import com.telepigeon.server.domain.Question;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;

public interface QuestionRepository extends JpaRepository<Question, Long> {
Optional<Question> findFirstByProfileOrderByCreatedAtDesc(Profile profile);

List<Question> findAllByProfile(Profile profile);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

import com.telepigeon.server.domain.Room;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.Optional;

public interface RoomRepository extends JpaRepository<Room, Long> {
boolean existsByName(String name);
boolean existsByCode(String code);

Optional<Room> findByCode(String code);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.telepigeon.server.repository;

import com.telepigeon.server.domain.Profile;
import com.telepigeon.server.domain.Worry;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface WorryRepository extends JpaRepository<Worry, Long> {
List<Worry> findAllByProfile(Profile profile);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.telepigeon.server.service.answer;

import com.telepigeon.server.domain.Answer;
import com.telepigeon.server.repository.AnswerRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class AnswerRemover {
private final AnswerRepository answerRepository;

public void remove(final Answer answer) {
answerRepository.delete(answer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public Answer findFirstByProfile(final Profile profile){
);
}

public List<Answer> findAllByProfile(final Profile profile) {
return answerRepository.findAllByProfile(profile);
}

public boolean existsByQuestion(final Question question){
return answerRepository.existsByQuestion(question);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.telepigeon.server.service.profile;

import com.telepigeon.server.domain.Profile;
import com.telepigeon.server.repository.ProfileRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class ProfileRemover {
private final ProfileRepository profileRepository;

public void remove(final Profile profile) {
profileRepository.delete(profile);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,30 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@RequiredArgsConstructor
public class ProfileRetriever {

private final ProfileRepository profileRepository;

public Profile findByUserAndRoom(Users user, Room room) {
public Profile findByUserAndRoom(final Users user, final Room room) {
return profileRepository.findByUserAndRoom(user, room)
.orElseThrow(() -> new NotFoundException(NotFoundErrorCode.PROFILE_NOT_FOUND));
}

public Profile findByUserNotAndRoom(Users user, Room room) {
public Profile findByUserNotAndRoom(final Users user, final Room room) {
return profileRepository.findByUserNotAndRoom(user, room)
.orElseThrow(() -> new NotFoundException(NotFoundErrorCode.PROFILE_NOT_FOUND));
}

public List<Profile> findByUserId(final long userId) {
return profileRepository.findAllByUserId(userId);
}

public List<Profile> findAll() {
return profileRepository.findAll();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.telepigeon.server.service.question;

import com.telepigeon.server.domain.Question;
import com.telepigeon.server.repository.QuestionRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class QuestionRemover {

private final QuestionRepository questionRepository;

public void remove(final Question question) {
questionRepository.delete(question);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;


@Component
@RequiredArgsConstructor
Expand All @@ -17,4 +19,8 @@ public Question findFirstByProfile(final Profile profile){
.findFirstByProfileOrderByCreatedAtDesc(profile)
.orElse(null); //예외 처리를 해주지 말아야 할 때도 있어서 null 반환
}

public List<Question> findAllByProfile(final Profile profile) {
return questionRepository.findAllByProfile(profile);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ public Room findById(final long id) {
.orElseThrow(() -> new NotFoundException(NotFoundErrorCode.ROOM_NOT_FOUND));
}

public Room findByCode(final String code) {
return roomRepository.findByCode(code)
.orElseThrow(() -> new NotFoundException(NotFoundErrorCode.ROOM_NOT_FOUND));
}

}
Loading

0 comments on commit c8c9c3f

Please sign in to comment.