Skip to content

Commit

Permalink
Merge pull request #6 from 32nd-GO-SOPT-SOPKATHON-13/feat/#4
Browse files Browse the repository at this point in the history
[FEAT] #4 - 유저 찌르기 조회 API 구현
  • Loading branch information
euije authored May 20, 2023
2 parents 0461ff6 + 208ac95 commit db909ee
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package sopt.sopkathon13.Server.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import sopt.sopkathon13.Server.common.dto.ApiResponse;
import sopt.sopkathon13.Server.controller.dto.response.TodayComplainResponseDto;
import sopt.sopkathon13.Server.exception.Success;
import sopt.sopkathon13.Server.service.UserService;

@RequiredArgsConstructor
@RequestMapping("/user")
@RestController
public class UserController {

private final UserService userService;

@GetMapping("/{fromHomeNumber}/complain")
@ResponseStatus(HttpStatus.OK)
public ApiResponse<TodayComplainResponseDto> readTodayComplain(@PathVariable("fromHomeNumber") int fromHomeNumber){
return ApiResponse.success(Success.COMPLAIN_FIND_SUCCESS, userService.findTodayComplain(fromHomeNumber));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package sopt.sopkathon13.Server.controller.dto.response;

import lombok.*;

import javax.validation.constraints.Size;
import java.util.List;

@Getter
@Builder
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class TodayComplainResponseDto {
@Size(max = 2)
private List<Long> up;

@Size(max = 2)
private List<Long> right;

@Size(max = 2)
private List<Long> left;

@Size(max = 2)
private List<Long> down;
private Long receiveCount;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public class Complain {
@ColumnDefault("0")
private Long complainCount;

@ManyToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "fromId", nullable = false, foreignKey = @ForeignKey(ConstraintMode.CONSTRAINT))
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "fromHomeNumber", nullable = false, foreignKey = @ForeignKey(ConstraintMode.CONSTRAINT))
private User fromUser;

@ManyToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "fromId", nullable = false, foreignKey = @ForeignKey(ConstraintMode.CONSTRAINT))
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "toHomeNumber", nullable = false, foreignKey = @ForeignKey(ConstraintMode.CONSTRAINT))
private User toUser;

public Complain(Long complainCount, User fromUser, User toUser) {
Expand Down
3 changes: 0 additions & 3 deletions Server/src/main/java/sopt/sopkathon13/Server/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long userId;

@Column(nullable = false)
private int homeNumber;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public enum Success {
GET_POST_SUCCESS(HttpStatus.OK, "게시물 조회에 성공했습니다."),
GET_EMOTION_CALENDAR_SUCCESS(HttpStatus.OK, "감정 캘린더 조회에 성공했습니다."),
GET_EMOTION_SUCCESS(HttpStatus.OK, "감정 조회에 성공했습니다."),
COMPLAIN_FIND_SUCCESS(HttpStatus.OK, "오늘 찌르기 조회에 성공하였습니다"),

/**
* 201 CREATED
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

import org.springframework.data.repository.Repository;
import sopt.sopkathon13.Server.domain.Complain;
import sopt.sopkathon13.Server.domain.User;

import java.util.List;

public interface ComplainRepository extends Repository<Complain, Long> {
// CREATE

// READ
List<Complain> findAllByToUser(User toUser);

// UPDATE

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import org.springframework.data.repository.Repository;
import sopt.sopkathon13.Server.domain.User;

public interface UserRepository extends Repository<User, Long> {
import java.util.List;

public interface UserRepository extends Repository<User, Integer> {
// CREATE

// READ
User findByHomeNumber(int homeNumber);

// UPDATE

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package sopt.sopkathon13.Server.service;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sopt.sopkathon13.Server.controller.dto.response.TodayComplainResponseDto;
import sopt.sopkathon13.Server.domain.Complain;
import sopt.sopkathon13.Server.domain.User;
import sopt.sopkathon13.Server.infrastructure.ComplainRepository;
import sopt.sopkathon13.Server.infrastructure.UserRepository;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class UserService {

private final UserRepository userRepository;
private final ComplainRepository complainRepository;

@Transactional
public TodayComplainResponseDto findTodayComplain(int fromHomeNumber){
User upUser = userRepository.findByHomeNumber(fromHomeNumber + 100);
User rightUser = userRepository.findByHomeNumber(fromHomeNumber + 1);
User downUser = userRepository.findByHomeNumber(fromHomeNumber - 100);
User leftUser = userRepository.findByHomeNumber(fromHomeNumber - 1);

List<Long> up = new ArrayList<>();
up.add((long)(fromHomeNumber + 100));
up.add(upUser.getTodayGetComplain());

List<Long> right = new ArrayList<>();
right.add((long)(fromHomeNumber + 1));
right.add(rightUser.getTodayGetComplain());

List<Long> down = new ArrayList<>();
down.add((long)(fromHomeNumber - 100));
down.add(downUser.getTodayGetComplain());

List<Long> left = new ArrayList<>();
left.add((long)(fromHomeNumber - 1));
left.add(leftUser.getTodayGetComplain());

User fromUser = userRepository.findByHomeNumber(fromHomeNumber);

return TodayComplainResponseDto.builder()
.up(up)
.right(right)
.down(down)
.left(left)
.receiveCount(fromUser.getTodayGetComplain())
.build();
}
}

0 comments on commit db909ee

Please sign in to comment.