Skip to content

Commit

Permalink
[FEAT] 열람실 잔여좌석 조회용 API 개발
Browse files Browse the repository at this point in the history
  • Loading branch information
SolfE authored Oct 25, 2024
1 parent 1d19a00 commit 1a6718c
Show file tree
Hide file tree
Showing 16 changed files with 228 additions and 12 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.jsoup:jsoup:1.16.1'
implementation 'com.h2database:h2'
implementation 'org.mapstruct:mapstruct:1.6.2'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.mysql:mysql-connector-j'
runtimeOnly 'com.h2database:h2'
annotationProcessor 'org.projectlombok:lombok'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.6.2'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/catspot/crawler/CrawlerScheduler.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.catspot.crawler;

import com.catspot.studyplace.StudyPlace;
import com.catspot.studyplace.StudyPlaceRepository;
import lombok.AllArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/catspot/crawler/LibraryCrawler.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.catspot.crawler;

import com.catspot.studyplace.StudyPlace;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class LibraryCrawler {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.catspot.crawler;
package com.catspot.studyplace;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;
Expand All @@ -18,10 +18,4 @@ public class StudyPlace {
private Integer allSeats;
private Integer useSeats;
private Integer restSeats;

@Override
public String toString() {
return "placeIdx=" + placeIdx + ", placeName=" + placeName + ", url=" + url +
", allSeats=" + allSeats + ", useSeats=" + useSeats + ", restSeats=" + restSeats;
}
}
28 changes: 28 additions & 0 deletions src/main/java/com/catspot/studyplace/StudyPlaceController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.catspot.studyplace;

import java.util.ArrayList;
import java.util.List;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
@AllArgsConstructor
public class StudyPlaceController {
private final StudyPlaceRepository studyPlaceRepository;

@GetMapping("/study-seat")
public StudyPlaceResponse getAllStudyPlaces() {
List<StudyPlace> allStudyPlaces = studyPlaceRepository.findAll();
List<StudyPlaceDto> studyPlaceDtos = new ArrayList<>();

for (StudyPlace studyPlace : allStudyPlaces) {
StudyPlaceDto dto = StudyPlaceMapper.INSTANCE.studyPlaceToStudyPlaceDto(studyPlace);
studyPlaceDtos.add(dto);
}

return StudyPlaceResponse.builder().data(studyPlaceDtos).build();
}
}
15 changes: 15 additions & 0 deletions src/main/java/com/catspot/studyplace/StudyPlaceDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.catspot.studyplace;

import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
public class StudyPlaceDto {
private Long placeIdx;
private String placeName;
private String url;
private Integer allSeats;
private Integer useSeats;
private Integer restSeats;
}
13 changes: 13 additions & 0 deletions src/main/java/com/catspot/studyplace/StudyPlaceMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.catspot.studyplace;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

@Mapper
public interface StudyPlaceMapper {

StudyPlaceMapper INSTANCE = Mappers.getMapper(StudyPlaceMapper.class);

StudyPlaceDto studyPlaceToStudyPlaceDto(StudyPlace studyPlace);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.catspot.crawler;
package com.catspot.studyplace;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/catspot/studyplace/StudyPlaceResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.catspot.studyplace;

import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

@Getter
@AllArgsConstructor
@Builder
public class StudyPlaceResponse {
private List<StudyPlaceDto> data;
}
2 changes: 1 addition & 1 deletion src/test/java/com/catspot/crawler/LibraryCrawlerTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.catspot.crawler;

import com.catspot.studyplace.StudyPlace;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.HashMap;
import java.util.List;

class LibraryCrawlerTest {
Expand Down
51 changes: 51 additions & 0 deletions src/test/java/com/catspot/studyplace/StudyPlaceControllerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.catspot.studyplace;

import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

@WebMvcTest(StudyPlaceController.class)
public class StudyPlaceControllerTest {
@MockBean
private StudyPlaceRepository studyPlaceRepository;

@Autowired
private MockMvc mockMvc;

@Test
void 열람실_데이터_조회() throws Exception {
// given
List<StudyPlace> response = studyPlaceResponse();
doReturn(response).when(studyPlaceRepository).findAll();

// then
ResultActions resultActions = mockMvc.perform(
MockMvcRequestBuilders.get("/api/study-seat"))
.andExpect(status().isOk());
}

private List<StudyPlace> studyPlaceResponse() {
List<StudyPlace> response = new ArrayList<StudyPlace>();
for (int i = 0; i < 5; i++) {
StudyPlace studyPlace = StudyPlace.builder()
.placeIdx((long) i)
.placeName("열람실A")
.url("https://열람실A.com")
.allSeats(100)
.useSeats(10)
.restSeats(90)
.build();
response.add(studyPlace);
}
return response;
}
}
20 changes: 20 additions & 0 deletions src/test/java/com/catspot/studyplace/StudyPlaceDtoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.catspot.studyplace;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class StudyPlaceDtoTest {
@Test
public void DTO_생성_테스트() {
StudyPlaceDto dto = StudyPlaceDto.builder()
.placeIdx(1L)
.placeName("1")
.url("url")
.allSeats(10)
.useSeats(2)
.restSeats(8)
.build();

Assertions.assertEquals(dto.getPlaceIdx(), 1);
}
}
43 changes: 43 additions & 0 deletions src/test/java/com/catspot/studyplace/StudyPlaceMapperTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.catspot.studyplace;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

public class StudyPlaceMapperTest {
@Test
public void DTO_매핑_테스트() {
StudyPlace studyPlace = getStudyPlace();

StudyPlaceDto studyPlaceDto = StudyPlaceMapper.INSTANCE.studyPlaceToStudyPlaceDto(studyPlace);

assertNotNull(studyPlaceDto);
assertEquals(studyPlaceDto.getPlaceIdx(), studyPlace.getPlaceIdx());
assertEquals(studyPlaceDto.getPlaceName(), studyPlace.getPlaceName());
assertEquals(studyPlaceDto.getUrl(), studyPlace.getUrl());
assertEquals(studyPlaceDto.getAllSeats(), studyPlace.getAllSeats());
assertEquals(studyPlaceDto.getUseSeats(), studyPlace.getUseSeats());
assertEquals(studyPlaceDto.getRestSeats(), studyPlace.getRestSeats());
}

@Test
public void DTO_null_매핑_테스트() {
StudyPlace studyPlace = null;

StudyPlaceDto studyPlaceDto = StudyPlaceMapper.INSTANCE.studyPlaceToStudyPlaceDto(studyPlace);

assertNull(studyPlaceDto);
}

private StudyPlace getStudyPlace() {
StudyPlace studyPlace = StudyPlace.builder()
.placeIdx(1L)
.placeName("1")
.url("url")
.allSeats(10)
.useSeats(2)
.restSeats(8)
.build();
return studyPlace;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.catspot.crawler;
package com.catspot.studyplace;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand Down
35 changes: 35 additions & 0 deletions src/test/java/com/catspot/studyplace/StudyPlaceResponseTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.catspot.studyplace;

import static org.junit.jupiter.api.Assertions.assertFalse;

import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class StudyPlaceResponseTest {
@Test
void 열람실_응답클래스_생성() {
List<StudyPlaceDto> studyPlaces = studyPlaceResponse();
StudyPlaceResponse studyPlaceResponse = new StudyPlaceResponse(studyPlaces);

assertFalse(studyPlaceResponse.getData().isEmpty());
}

private List<StudyPlaceDto> studyPlaceResponse() {
List<StudyPlaceDto> response = new ArrayList<>();
for (int i = 0; i < 5; i++) {
StudyPlaceDto studyPlaceDto = StudyPlaceDto.builder()
.placeIdx((long) i)
.placeName("열람실A")
.url("https://열람실A.com")
.allSeats(100)
.useSeats(10)
.restSeats(90)
.build();

response.add(studyPlaceDto);
}
return response;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.catspot.crawler;
package com.catspot.studyplace;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand Down

0 comments on commit 1a6718c

Please sign in to comment.