Skip to content

Commit

Permalink
feat: UserController 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
ezzanzzan committed Sep 25, 2023
1 parent 073989e commit 27dd1c9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
31 changes: 31 additions & 0 deletions app/src/main/java/com/techcourse/controller/UserController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.techcourse.controller;

import com.techcourse.domain.User;
import com.techcourse.repository.InMemoryUserRepository;
import context.org.springframework.stereotype.Controller;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import web.org.springframework.web.bind.annotation.RequestMapping;
import web.org.springframework.web.bind.annotation.RequestMethod;
import webmvc.org.springframework.web.servlet.ModelAndView;
import webmvc.org.springframework.web.servlet.view.JsonView;

@Controller
public class UserController {
private static final Logger log = LoggerFactory.getLogger(UserController.class);

@RequestMapping(value = "/api/user", method = RequestMethod.GET)
public ModelAndView show(HttpServletRequest req, HttpServletResponse res) {
final String account = req.getParameter("account");
log.debug("user id : {}", account);

final ModelAndView modelAndView = new ModelAndView(new JsonView());
final User user = InMemoryUserRepository.findByAccount(account)
.orElseThrow();

modelAndView.addObject("user", user);
return modelAndView;
}
}
4 changes: 2 additions & 2 deletions app/src/main/java/com/techcourse/controller/UserSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.Optional;

public class UserSession {

public static final String SESSION_KEY = "user";

public static Optional<User> getUserFrom(final HttpSession session) {
Expand All @@ -18,5 +17,6 @@ public static boolean isLoggedIn(final HttpSession session) {
return getUserFrom(session).isPresent();
}

private UserSession() {}
private UserSession() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,15 @@
public class JsonView implements View {
@Override
public void render(final Map<String, ?> model, final HttpServletRequest request, HttpServletResponse response) throws Exception {
final String body = bodyToJson(getBody(model));
final String body = bodyToJson(model);
response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
response.getWriter().write(body);
}

private Object getBody(final Map<String, ?> model) {
if (model.size() == 1) {
return model.keySet().toArray()[0];
}
return model;
}

private String bodyToJson(final Object body) throws JsonProcessingException {
final ObjectMapper objectMapper = new ObjectMapper();
return objectMapper
.writerWithDefaultPrettyPrinter()
.writeValueAsString(body);
}

}

0 comments on commit 27dd1c9

Please sign in to comment.