2 분 소요

image

두 번째 과제

[인프런 워밍업 클럽 1기] BE 2일차 과제

문제 1

image

구현 과정

  • 구현 목표 : 두 수를 입력해 요청하면 덧셈, 뺄셈, 곱셈의 결과를 반환하여 응답함.
  • 메서드 타입 : GET
  • 경로 : /api/v1/calc
  • 쿼리 파라미터
    • 이름 : num1 / 타입 : int
    • 이름 : num2 / 타입 : int

Controller 구현 - CalcController.java

package com.group.libraryapp.controller.assignment.calc;
import com.group.libraryapp.dto.assignment.request.CalculatorCalcRequest;
import com.group.libraryapp.dto.assignment.response.CalculatorCalcResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CalcController {

    @GetMapping("/api/v1/calc")
    public CalculatorCalcResponse calcTwoNumbers(CalculatorCalcRequest request ) {
        return new CalculatorCalcResponse(request);
    }
}

Request DTO 구현 - CalculatorCalcRequest.java

package com.group.libraryapp.dto.assignment.request;

public class CalculatorCalcRequest {

    private int num1;
    private int num2;

    public CalculatorCalcRequest(int num1, int num2) {
        this.num1 = num1;
        this.num2 = num2;
    }

    public int getNum1() {
        return num1;
    }

    public int getNum2() {
        return num2;
    }
}

Response DTO 구현 - CalculatorCalcResponse

package com.group.libraryapp.dto.assignment.response;

import com.group.libraryapp.dto.assignment.request.CalculatorCalcRequest;

public class CalculatorCalcResponse {
    private int add;
    private int minus;
    private int multiply;

    public CalculatorCalcResponse(CalculatorCalcRequest num) {
        this.add = num.getNum1() + num.getNum2();
        this.minus = num.getNum1() - num.getNum2();
        this.multiply = num.getNum1() * num.getNum2();
    }

    public int getAdd() {
        return add;
    }

    public int getMinus() {
        return minus;
    }

    public int getMultiply() {
        return multiply;
    }
}

서버 실행 후 Postman 결과 확인

image

문제 2

image

구현 과정

  • 구현 목표 : 날짜를 입력해 요청하면 해당 날짜 요일을 반환하여 응답함.
  • 메서드 타입 : GET
  • 경로 : /api/v1/day-of-the-week
  • 쿼리 파라미터
    • 이름 : date / 타입 : LocalDate
    • Controller 구현 - dayController.java

      ```java package com.group.libraryapp.controller.assignment.day;

import com.group.libraryapp.dto.assignment.response.DayOfTheWeekResponse; import org.springframework.format.annotation.DateTimeFormat; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;

@RestController public class dayController {

@GetMapping("/api/v1/day-of-the-week")
public DayOfTheWeekResponse getDayOfWeek(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date) {
    return new DayOfTheWeekResponse(date);
} } ``` ### Request DTO 구현 - DayOfTheWeekRequest.java ```java package com.group.libraryapp.dto.assignment.request;

import java.time.LocalDate;

public class DayOfTheWeekRequest {

private LocalDate date;

public DayOfTheWeekRequest(LocalDate date) {
    this.date = date;
}

public LocalDate getDate() {
    return date;
} } ```

Response DTO 구현 - DayOfTheWeekResponse.java

package com.group.libraryapp.dto.assignment.response;

import java.time.LocalDate;

public class DayOfTheWeekResponse {
    private String dayOfTheWeek;

    public DayOfTheWeekResponse(LocalDate date) {
        this.dayOfTheWeek = date.getDayOfWeek().toString();
    }

    public String getDayOfTheWeek() {
        return dayOfTheWeek.substring(0, 3);
    }
}

서버 실행 후 Postman 결과 확인

image

문제 3

image

구현 과정

  • 구현 목표 : 여러 수가 입력된 배열을 입력해 요청하면 배열 내 수의 총합을 반환하여 응답함.
  • 메서드 타입 : POST
  • 경로 : /totalsum
  • 쿼리 파라미터
    • 이름 : numbers / 타입 : List

Controller 구현 - TotalSumController.java

package com.group.libraryapp.controller.assignment.totalsum;

import com.group.libraryapp.dto.assignment.request.TotalSumRequest;
import com.group.libraryapp.dto.assignment.response.TotalSumResponse;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TotalSumController {

    @PostMapping("/totalsum")
    public TotalSumResponse getTotalSum(@RequestBody TotalSumRequest request) {
        return new TotalSumResponse(request);
    }
}

Request DTO 구현 - TotalSumRequest.java

package com.group.libraryapp.dto.assignment.request;

import java.util.List;

public class TotalSumRequest {

    private List<Integer> numbers;

    public List<Integer> getNumbers() {
        return numbers;
    }
}

Response DTO 구현 - TotalSumResponse.java

package com.group.libraryapp.dto.assignment.response;

import com.group.libraryapp.dto.assignment.request.TotalSumRequest;

public class TotalSumResponse {

    private int sum;

    public int getSum() {
        return sum;
    }

    public TotalSumResponse(TotalSumRequest request) {
        this.sum = request.getNumbers().stream().mapToInt(Integer::intValue).sum();
    }
}

서버 실행 후 Postman 결과 확인

image

 

댓글남기기