Skip to content

Commit

Permalink
feat: 특정인의 오름 정보 조회
Browse files Browse the repository at this point in the history
  • Loading branch information
Song-EunJu committed Nov 17, 2022
1 parent 72ac8ea commit d69ac35
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 27 deletions.
3 changes: 1 addition & 2 deletions src/main/java/com/example/goorm/oreum/MyOreum.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public class MyOreum {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String name;
private String nickname;
private Long oreumId;
}
13 changes: 6 additions & 7 deletions src/main/java/com/example/goorm/oreum/Oreum.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.Arrays;

Expand All @@ -22,10 +19,12 @@ public class Oreum {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;

@Enumerated(EnumType.STRING)
private OreumType type;
private Double xPos;
private Double yPos;
private Double zPos;
private double xPos;
private double yPos;
private double zPos;
private int month;
private int day;

Expand Down
21 changes: 12 additions & 9 deletions src/main/java/com/example/goorm/oreum/OreumController.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.example.goorm.oreum;

import com.example.goorm.oreum.dto.BirthDayRequest;
import com.example.goorm.oreum.dto.OreumResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

Expand All @@ -17,20 +19,21 @@ public class OreumController {

private final OreumService oreumService;

@GetMapping("/oreums")
public ResponseEntity<List<Oreum>> getData(){
return ResponseEntity.ok().body(oreumService.getOreums());
}

@PostMapping("/oreums")
public ResponseEntity<String> pushData(){
oreumService.readCsv();
return ResponseEntity.ok().body("완성");
}

// 생일에 따른 오름 정보 보여주기
@GetMapping("/oreum")
public ResponseEntity<Oreum> getOreum(@RequestBody BirtyDayRequest request){
// 생일에 따른 오름 정보 보여주기 (입력하고 디비에 저장하는 거까지)
@PostMapping("/oreum")
public ResponseEntity<OreumResponse> getOreum(@RequestBody BirthDayRequest request){
return ResponseEntity.ok().body(oreumService.getOreum(request));
}

// 내가 받았던 오름 정보 보여주기 (/oreums/1) (카톡 공유했을 때 보여주는 api)
@GetMapping("/oreums/{myOreumId}")
public ResponseEntity<OreumResponse> findByName(@PathVariable("myOreumId") Long myOreumId) {
return ResponseEntity.ok().body(oreumService.getMyOreum(myOreumId));
}
}
33 changes: 28 additions & 5 deletions src/main/java/com/example/goorm/oreum/OreumService.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,49 @@
package com.example.goorm.oreum;

import com.example.goorm.oreum.dto.BirthDayRequest;
import com.example.goorm.oreum.dto.OreumResponse;
import com.example.goorm.oreum.repository.MyOreumRepository;
import com.example.goorm.oreum.repository.OreumRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Service
@RequiredArgsConstructor
public class OreumService {
private final OreumRepository oreumRepository;
private final MyOreumRepository myOreumRepository;

public List<Oreum> getOreums(){
return oreumRepository.findAll();
}

public Oreum getOreum(BirtyDayRequest request){
public OreumResponse getOreum(BirthDayRequest request){
int month = request.getMonth();
int day = request.getDay();
return oreumRepository.findByBirthday(month, day);
Oreum oreum = oreumRepository.findByBirthday(month, day); // 저장된 오름을 찾아서
MyOreum myOreum = MyOreum.builder() // 나의 오름을 저장하고
.nickname(request.getNickname())
.oreumId(oreum.getId())
.build();

myOreumRepository.save(myOreum);
return OreumResponse.of(oreum, myOreum, request);
}

public OreumResponse getMyOreum(Long myOreumId){
MyOreum myOreum = myOreumRepository.findById(myOreumId).orElseThrow(() -> {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "해당하는 오름 스티커 번호가 없습니다");
});
Oreum oreumInfo = oreumRepository.findById(myOreum.getOreumId()).orElseThrow(() -> {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "해당하는 오름의 번호가 없습니다");
});
return OreumResponse.ofOthers(oreumInfo, myOreum);
}

public void readCsv(){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.goorm.oreum;
package com.example.goorm.oreum.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
Expand All @@ -9,7 +9,8 @@
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class BirtyDayRequest {
public class BirthDayRequest {
private String nickname;
private int month;
private int day;
}
}
44 changes: 44 additions & 0 deletions src/main/java/com/example/goorm/oreum/dto/OreumResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.example.goorm.oreum.dto;

import com.example.goorm.oreum.MyOreum;
import com.example.goorm.oreum.Oreum;
import com.example.goorm.oreum.OreumType;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Builder
public class OreumResponse {
private String nickname;
private String name;
private OreumType type;
private double xPos;
private double yPos;
private double zPos;
private Long myOreumId;

public static OreumResponse of(Oreum oreum, MyOreum myOreum, BirthDayRequest request){
return OreumResponse.builder()
.nickname(request.getNickname())
.name(oreum.getName())
.type(oreum.getType())
.xPos(oreum.getXPos())
.yPos(oreum.getYPos())
.zPos(oreum.getZPos())
.myOreumId(myOreum.getId())
.build();
}

public static OreumResponse ofOthers(Oreum oreum, MyOreum myOreum){
return OreumResponse.builder()
.nickname(myOreum.getNickname())
.name(oreum.getName())
.type(oreum.getType())
.xPos(oreum.getXPos())
.yPos(oreum.getYPos())
.zPos(oreum.getZPos())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.goorm.oreum.repository;

import com.example.goorm.oreum.MyOreum;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface MyOreumRepository extends JpaRepository<MyOreum, Long> {
Optional<MyOreum> findById(Long id);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.goorm.oreum;
package com.example.goorm.oreum.repository;

import com.example.goorm.oreum.Oreum;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

Expand Down

0 comments on commit d69ac35

Please sign in to comment.