Skip to content

Interview: Код ревью на SessionController #263

Open
@aarexer

Description

Разместить в interview/code_review/medium

Дан следующий класс:

package interview.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
import interview.model.User;
import interview.repository.UserRepository;
import interview.util.MailSenderUtil;

import java.util.Optional;
import java.util.UUID;
import java.util.Random;
import java.util.stream.Collectors;

@RestController
public class SessionController {
    @Autowired
    UserRepository userRepository;

    @GetMapping("/session")
    private void getSession(@RequestParam Integer userId) {
        Optional<User> user = userRepository.findById(userId);
        String code = new Random()
            .ints(0, 9)
            .limit(6)
            .mapToObj(String::valueOf)
            .collect(Collectors.joining());

        if (user.isPresent()) {
            user.get().setConfirmationCode(code);
            MailSenderUtil.sendEmail(user.get().getEmail(), "Enter code: " + code);
            userRepository.save(user.get());
        } else {
            throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "User not found");
        }
    }

    @PostMapping("/session/confirm")
    private User confirmSession(@RequestParam Integer userId, @RequestParam String code) {
        Optional<User> user = userRepository.findById(userId);
        String session = UUID.randomUUID().toString();

        if (user.isPresent()) {
            if (code.equals(user.get().getConfirmationCode())) {
                user.get().setSession(session);
                userRepository.save(user.get());
                return user.get();
            } else {
                throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "Wrong code");
            }
        } else {
            throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "User not found");
        }
    }
}

Необходимо провести и расписать проблемы как на code review, с пояснениями.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    interviewAbout interview tasks

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions