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

M3-117 개인전 픽셀 가져오는 API 구현 완료 #4

Merged
merged 5 commits into from
Jun 25, 2024
Merged
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
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1 +1,31 @@
package com.m3pro.groundflip.controller;import org.springframework.web.bind.annotation.RestController;import com.m3pro.groundflip.service.PixelService;import lombok.RequiredArgsConstructor;@RestController@RequiredArgsConstructorpublic class PixelController { private final PixelService pixelService;}
package com.m3pro.groundflip.controller;

import java.util.List;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.m3pro.groundflip.domain.dto.Response;
import com.m3pro.groundflip.domain.dto.pixel.IndividualPixelResponse;
import com.m3pro.groundflip.service.PixelService;

import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/pixels")
Copy link
Member

Choose a reason for hiding this comment

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

개인 픽셀이랑 경쟁 픽셀이랑 구분하기 위해 엔드포인트에 구분자가 필요할 것 같네요..

Copy link
Contributor Author

Choose a reason for hiding this comment

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

적용 완료했습니다!

public class PixelController {
private final PixelService pixelService;

@GetMapping("/individual")
public Response<List<IndividualPixelResponse>> getNearIndividualPixels(
@RequestParam(name = "current-x") int currentX,
@RequestParam(name = "current-y") int currentY,
@RequestParam(name = "x-range", required = false, defaultValue = "20") int xRange,
@RequestParam(name = "y-range", required = false, defaultValue = "10") int yRange) {
return Response.createSuccess(pixelService.getNearIndividualPixels(currentX, currentY, xRange, yRange));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.m3pro.groundflip.domain.dto.pixel;

import com.m3pro.groundflip.domain.entity.Pixel;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@NoArgsConstructor
@Data
public class IndividualPixelResponse {
private double latitude;
private double longitude;
private double x;
private double y;

public static IndividualPixelResponse from(Pixel pixel) {
return new IndividualPixelResponse(pixel.getLatitude(), pixel.getLongitude(), pixel.getX(), pixel.getY());
}
}
3 changes: 2 additions & 1 deletion src/main/java/com/m3pro/groundflip/domain/entity/Pixel.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Index;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
Expand All @@ -19,7 +20,7 @@

@Getter
@Entity
@Table(name = "pixel")
@Table(name = "pixel", indexes = @Index(name = "index__x__y", columnList = "x, y"))
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@Builder
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
package com.m3pro.groundflip.repository;import org.springframework.data.jpa.repository.JpaRepository;import com.m3pro.groundflip.domain.entity.Pixel;public interface PixelRepository extends JpaRepository<Pixel, Long> {}
package com.m3pro.groundflip.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.m3pro.groundflip.domain.entity.Pixel;

public interface PixelRepository extends JpaRepository<Pixel, Long> {
@Query(value = "select pixel from Pixel pixel "
+ "where pixel.x between :currentX - :xRange / 2 and :currentX + :xRange / 2 "
+ "and pixel.y between :currentY - :yRange / 2 and :currentY + :yRange / 2 ")
List<Pixel> findAllNearPixels(@Param("currentX") int currentX, @Param("currentY") int currentY,
@Param("xRange") int xRange, @Param("yRange") int yRange);
}
24 changes: 23 additions & 1 deletion src/main/java/com/m3pro/groundflip/service/PixelService.java
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
package com.m3pro.groundflip.service;import org.springframework.stereotype.Service;import lombok.RequiredArgsConstructor;@Service@RequiredArgsConstructorpublic class PixelService {}
package com.m3pro.groundflip.service;

import java.util.List;

import org.springframework.stereotype.Service;

import com.m3pro.groundflip.domain.dto.pixel.IndividualPixelResponse;
import com.m3pro.groundflip.repository.PixelRepository;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class PixelService {
private final PixelRepository pixelRepository;

public List<IndividualPixelResponse> getNearIndividualPixels(int currentX, int currentY, int xRange, int yRange) {
Copy link
Member

Choose a reason for hiding this comment

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

개인 픽셀과 경쟁 픽셀을 조회 하는 구분이 필요할 것 같아요

Copy link
Contributor Author

Choose a reason for hiding this comment

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

생각하지 못했던 구분이였네요.. 추후 동적 쿼리나 컨트롤러 단에 구분자 추가 등으로 적용하겠습니다.

return pixelRepository.findAllNearPixels(currentX, currentY, xRange, yRange)
.stream()
.map(IndividualPixelResponse::from)
.toList();
}
}
Loading