[인프런 워밍업 클럽_0기 BE] 두번째 과제  - API

[인프런 워밍업 클럽_0기 BE] 두번째 과제 - API

우리는 GET API와 POST API를 만드는 방법을 배웠습니다. 👍

추가적인 API 들을 만들어 보며 API 개발에 익숙해져 봅시다!


문제 풀이)
image

<no1 Controller>

package com.group.libarayapp.controller.task;
import com.group.libarayapp.dto.task.request.Ex01Request;
import com.group.libarayapp.dto.task.response.Ex01Response;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Ex01Controller { @GetMapping("/api/v1/calc")
 public Ex01Response firstEx(Ex01Request request) {
 return new Ex01Response(request);
  }
}

<no.1 Request>

package com.group.libarayapp.dto.task.request;
public class Ex01Request {
private final int num1;
private final int num2;
public Ex01Request(int num1, int num2) {

this.num1 = num1;
this.num2 = num2;
 }

public int getNum1() {

 return num1;  }

public int getNum2() {
return num2;
  }
}

<no.1 Response>

package com.group.libarayapp.dto.task.response;

import com.group.libarayapp.dto.task.request.Ex01Request;

public class Ex01Response {

    private final int add;
    private final int minus;
    private final int multiply;


    public Ex01Response(Ex01Request ex01Request) {
        this.add = ex01Request.getNum1() + ex01Request.getNum2();
        this.minus = ex01Request.getNum1() - ex01Request.getNum2();
        this.multiply = ex01Request.getNum1() * ex01Request.getNum2();
    }

    public int getAdd() {
        return add;
    }

    public int getMinus() {
        return minus;
    }

    public int getMultiply() {
        return multiply;
    }
}

image<no2. Controller>

package com.group.libarayapp.controller.task;

import com.group.libarayapp.dto.task.request.Ex02Request;
import com.group.libarayapp.dto.task.response.Ex02Response;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Ex02Controller {

    @GetMapping("/api/v1/day-of-the-week")
    public Ex02Response secondEx(Ex02Request request) {
        return new Ex02Response(request);
    }
}

<no2. Request>

package com.group.libarayapp.dto.task.request;

import java.time.LocalDate;

public class Ex02Request {

    private final LocalDate date;

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

    public LocalDate getDate() {
        return date;
    }
}

<no2. Response>

package com.group.libarayapp.dto.task.response;

import com.group.libarayapp.dto.task.request.Ex02Request;

import java.time.format.TextStyle;
import java.util.Locale;

public class Ex02Response {

    private final String dayOfTheWeek;

    public Ex02Response(Ex02Request request) {
        this.dayOfTheWeek = request.getDate().getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.US).toUpperCase();
    }

    public String getDayOfTheWeek() {
        return dayOfTheWeek;
    }
}

 

*LocalDate : 로컬 시간 클래스로 시간 정보가 필요할 때 사용

// 로컬 컴퓨터의 현재 시간 정보를 저장한 LocalDate 객체를 리턴.

LocalTime currentTime = LocalTime.now();

 

// 파라미터로 주어진 시간 정보를 저장한 LocalTime 객체를 리턴.

LocalTime targetTime = LocalTime.of(12,33,35,22);

 

image<no3. Controller>

package com.group.libarayapp.controller.task;


import com.group.libarayapp.dto.task.request.Ex03Request;
import com.group.libarayapp.dto.task.response.Ex03Response;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Ex03Controller {

    @PostMapping("/api/v1/numbers-sum")
    public int thirdEx(@RequestBody Ex03Request request) {
        Ex03Response result = new Ex03Response(request.getNumbers());
        return result.getSum();
    }
}

 

<no3. Request>

package com.group.libarayapp.dto.task.request;

import java.util.ArrayList;
import java.util.List;

public class Ex03Request {

    private final List<Integer> numbers = new ArrayList<>();

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


}

<no3. Response>

package com.group.libarayapp.dto.task.response;

import java.util.List;

public class Ex03Response {
    private int sum;

    public Ex03Response(List<Integer> numbers) {
        for (int number : numbers) {
            sum += number;
        }
    }

    public int getSum() {
        return sum;
    }
}


참고 출처 : https://java119.tistory.com/52

댓글을 작성해보세요.

채널톡 아이콘