🔥새해맞이 특별 라이브 선착순 신청🔥

[인프런 워밍업 스터디 클럽 0기 Back] API 작성 2일차 미션

package com.example.springlibraryapp.controller.calculator;

import com.example.springlibraryapp.dto.calculator.request.CalculatorAddRequest;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.springframework.web.bind.annotation.*;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.TextStyle;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

@RestController
public class Controller {
    // 1번 문제
    @GetMapping("/api/v1/calc")
    public calculateTwoNumbersResult calculateTwoNumbers(CalculatorAddRequest request) {
        int addResult = request.getNumber1() + request.getNumber2();
        int minusResult = request.getNumber1() - request.getNumber2();
        int multiplyResult = request.getNumber1() * request.getNumber2();
        
        return new calculateTwoNumbersResult(addResult, minusResult, multiplyResult);
    }
    
    @Getter
    @RequiredArgsConstructor
    static class calculateTwoNumbersResult {
        private final int add;
        private final int minus;
        private final int multiply;
    }
    
    
    // 2번문제
    @GetMapping("/api/v1/day-of-the-week")
    public DayOfTheWeekResponse getDayOfWeek(@RequestParam(value = "date") String dateString) {
        LocalDate date = LocalDate.parse(dateString);
        DayOfWeek dayOfWeek = date.getDayOfWeek();
        
        String dayOfWeekUpperCase = dayOfWeek.getDisplayName(TextStyle.SHORT, Locale.ENGLISH).toUpperCase();
        
        return new DayOfTheWeekResponse(dayOfWeekUpperCase);
    }
    
    @Getter @Setter
    @RequiredArgsConstructor
    static class DayOfTheWeekResponse {
        private final String dayOfTheWeek;
    }
    
    
    // 3번 문제
    @PostMapping("/api/post")
    public int postSumArray(@RequestBody PostSumArrayRequest request) {
        List<Integer> numbers = request.getNumbers();
        
        int result = 0;
        
        for (Integer number : numbers) {
            result += number;
        }
        
        return result;
    }
    
    @Getter @Setter
    static class PostSumArrayRequest {
        
        private ArrayList<Integer> numbers;
    }
}

 

댓글을 작성해보세요.

채널톡 아이콘