Skip to content

Commit

Permalink
Merge pull request #17 from f-lab-edu/feature/15-schedules
Browse files Browse the repository at this point in the history
[#15] 경기 목록 조회 API 구현
  • Loading branch information
princenim authored Apr 1, 2024
2 parents 9a2458b + 1cbe74f commit 0357cc1
Show file tree
Hide file tree
Showing 8 changed files with 229 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.kboticketing.kboticketing.controller;


import com.kboticketing.kboticketing.domain.ScheduleTeam;
import com.kboticketing.kboticketing.dto.ScheduleQueryParamDto;
import com.kboticketing.kboticketing.service.ScheduleService;
import com.kboticketing.kboticketing.utils.response.CommonResponse;
import java.util.ArrayList;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* @author hazel
*/
@RestController
@RequiredArgsConstructor
public class ScheduleController {

private final ScheduleService scheduleService;

@GetMapping("schedules")
public ResponseEntity<CommonResponse> getSchedules(
ScheduleQueryParamDto scheduleQueryParamDto) {
ArrayList<ScheduleTeam> schedules = scheduleService.getSchedules(scheduleQueryParamDto);
return ResponseEntity.ok(CommonResponse.ok(schedules));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.kboticketing.kboticketing.dao;

import com.kboticketing.kboticketing.domain.ScheduleTeam;
import com.kboticketing.kboticketing.dto.ScheduleQueryParamDto;
import java.util.ArrayList;
import org.apache.ibatis.annotations.Mapper;

/**
* @author hazel
*/
@Mapper
public interface ScheduleMapper {

ArrayList<ScheduleTeam> selectSchedules(ScheduleQueryParamDto scheduleQueryParamDto);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.kboticketing.kboticketing.domain;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

/**
* @author hazel
*/
@RequiredArgsConstructor
@Getter
public class ScheduleTeam {

private final Integer scheduleId;
private final String scheduleName;
private final String stadiumName;
private final String date;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.kboticketing.kboticketing.dto;

import java.time.LocalDateTime;
import lombok.Getter;

/**
* @author hazel
*/
@Getter
public class ScheduleQueryParamDto {

private final Integer teamId;
private final Integer month;
private final Integer day;
private final Integer stadiumId;

public ScheduleQueryParamDto(Integer teamId, Integer month, Integer day, Integer stadiumId) {
this.teamId = teamId;
// month 필드가 null이라면 현재 월 설정
this.month = month == null ? LocalDateTime.now()
.getMonthValue() : month;
this.day = day;
this.stadiumId = stadiumId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.kboticketing.kboticketing.service;

import com.kboticketing.kboticketing.dao.ScheduleMapper;
import com.kboticketing.kboticketing.domain.ScheduleTeam;
import com.kboticketing.kboticketing.dto.ScheduleQueryParamDto;
import java.util.ArrayList;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
* @author hazel
*/
@Service
@RequiredArgsConstructor
public class ScheduleService {

private final ScheduleMapper scheduleMapper;

public ArrayList<ScheduleTeam> getSchedules(ScheduleQueryParamDto scheduleQueryParamDto) {
return scheduleMapper.selectSchedules(scheduleQueryParamDto);
}
}
30 changes: 30 additions & 0 deletions src/main/resources/mapper/ScheduleMapper.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.kboticketing.kboticketing.dao.ScheduleMapper">
<select id="selectSchedules" resultType="com.kboticketing.kboticketing.domain.ScheduleTeam">
SELECT
schedules.schedule_id,
schedules.name AS scheduleName,
stadium.name AS stadiumName,
schedules.date AS date
FROM schedules schedules
LEFT JOIN
stadium ON stadium.stadium_id = schedules.stadium_id
<where>
<if test="teamId != null">
AND home_team_id = #{teamId}
</if>
<if test="month != null">
AND month(date)= #{month}
</if>
<if test="day != null">
AND day(date)= #{day}
</if>
<if test="stadiumId != null">
AND schedules.stadium_id = #{stadiumId}
</if>
</where>
</select>
</mapper>
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.kboticketing.kboticketing.controller;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.kboticketing.kboticketing.domain.ScheduleTeam;
import com.kboticketing.kboticketing.service.ScheduleService;
import java.util.ArrayList;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
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;

/**
* @author hazel
*/
@WebMvcTest(ScheduleController.class)
@ExtendWith(MockitoExtension.class)
class ScheduleControllerTest {

@Autowired
private MockMvc mockMvc;

@MockBean
ScheduleService scheduleService;

@Test
@DisplayName("[SUCCESS] 일정 목록 조회 테스트")
public void getSchedulesTest() throws Exception {

//given
ArrayList<ScheduleTeam> scheduleTeams = new ArrayList<>();
scheduleTeams.add(new ScheduleTeam(1, "LG vs SSG", "잠실종합운동장", "2024-04-01 18:30:00"));
given(scheduleService.getSchedules(any())).willReturn(scheduleTeams);

//when, then
mockMvc.perform(get("/schedules"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.data[0].scheduleId").value(scheduleTeams.get(0)
.getScheduleId()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.kboticketing.kboticketing.service;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.mockito.ArgumentMatchers.any;

import com.kboticketing.kboticketing.dao.ScheduleMapper;
import com.kboticketing.kboticketing.domain.ScheduleTeam;
import java.util.ArrayList;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.mockito.BDDMockito.given;

/**
* @author hazel
*/
@ExtendWith(MockitoExtension.class)
class ScheduleServiceTest {

@Mock
private ScheduleMapper scheduleMapper;

@InjectMocks
private ScheduleService scheduleService;

@Test
@DisplayName("[SUCCESS] 경기 목록 조회 테스트")
public void getSchedulesTest() {

//given
ArrayList<ScheduleTeam> scheduleTeams = new ArrayList<>();
scheduleTeams.add(new ScheduleTeam(1, "LG vs SSG", "잠실종합운동장", "2024-04-01 18:30:00"));
given(scheduleMapper.selectSchedules(any())).willReturn(scheduleTeams);

//when, then
assertThat(scheduleService.getSchedules(any())).isEqualTo(scheduleTeams);
}
}

0 comments on commit 0357cc1

Please sign in to comment.