Skip to content

Commit

Permalink
chore: resolve conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
Kim0914 committed Sep 12, 2023
2 parents 66bd282 + 0b07af2 commit c93059d
Show file tree
Hide file tree
Showing 40 changed files with 1,827 additions and 15 deletions.
2 changes: 2 additions & 0 deletions backend/pium/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect'

implementation 'com.github.maricn:logback-slack-appender:1.6.1'
implementation 'com.querydsl:querydsl-jpa:5.0.0:jakarta'
Expand Down
22 changes: 22 additions & 0 deletions backend/pium/src/docs/asciidoc/dictionaryPlant.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,25 @@ include::{snippets}/dictionaryPlant/findByName/query-parameters.adoc[]
==== Response

include::{snippets}/dictionaryPlant/findByName/http-response.adoc[]

=== 사전 식물 정보 수정

==== Request

include::{snippets}/dictionaryPlant/update/http-request.adoc[]
include::{snippets}/dictionaryPlant/update/path-parameters.adoc[]

==== Response

include::{snippets}/dictionaryPlant/findByName/http-response.adoc[]


=== 사전 식물 삭제

==== Request

include::{snippets}/dictionaryPlant/delete/http-request.adoc[]
include::{snippets}/dictionaryPlant/delete/path-parameters.adoc[]
==== Response

include::{snippets}/dictionaryPlant/delete/http-response.adoc[]
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.official.pium.config;

import com.official.pium.controller.AdminArgumentResolver;
import com.official.pium.controller.MemberArgumentResolver;
import com.official.pium.repository.MemberRepository;
import java.util.List;
Expand All @@ -17,5 +18,6 @@ public class WebMvcConfigure implements WebMvcConfigurer {
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
resolvers.add(new MemberArgumentResolver(memberRepository));
resolvers.add(new AdminArgumentResolver());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.official.pium.controller;

import com.official.pium.domain.Admin;
import com.official.pium.domain.AdminAuth;
import com.official.pium.exception.AuthorizationException;
import com.official.pium.exception.AuthorizationException.NeedAdminException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;


public class AdminArgumentResolver implements HandlerMethodArgumentResolver {

private static final String SESSION_KEY = "PIUM_ADMIN_SESSION_ID";

@Override
public boolean supportsParameter(MethodParameter parameter) {
return parameter.hasParameterAnnotation(AdminAuth.class) && parameter.getParameterType().equals(Admin.class);
}

@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) {
HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest();
HttpSession session = request.getSession(false);

if (session == null) {
return null;
}

try {
Admin admin = (Admin) session.getAttribute(SESSION_KEY);
if (admin == null) {
throw new NeedAdminException("관리자 권한이 필요합니다.");
}
return admin;
} catch (Exception e) {
throw new AuthorizationException("잘못된 세션정보입니다.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package com.official.pium.controller;

import com.official.pium.domain.Admin;
import com.official.pium.domain.AdminAuth;
import com.official.pium.domain.DictionaryPlant;
import com.official.pium.repository.DictionaryPlantRepository;
import com.official.pium.service.AdminService;
import com.official.pium.service.dto.AdminLoginRequest;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.Valid;
import java.util.NoSuchElementException;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

@Validated
@Controller
@RequiredArgsConstructor
@RequestMapping("/admin")
public class AdminPageController {

private static final String REDIRECT_ADMIN_LOGIN = "redirect:/admin/login";

private final DictionaryPlantRepository dictionaryPlantRepository;
private final AdminService adminService;

@GetMapping("/**")
public String adminPage(@AdminAuth Admin admin, Model model) {
if (admin == null) {
return REDIRECT_ADMIN_LOGIN;
}

model.addAttribute("admin", admin);
return "/admin/index";
}

@GetMapping("/dict")
public String dictionaryPlants(@PageableDefault Pageable pageable, @AdminAuth Admin admin, Model model) {
if (admin == null) {
return REDIRECT_ADMIN_LOGIN;
}

Page<DictionaryPlant> dictionaryPlants = dictionaryPlantRepository.findAll(pageable);
model.addAttribute("admin", admin);
model.addAttribute("page", dictionaryPlants);
model.addAttribute("plants", dictionaryPlants.getContent());
return "/admin/dict/list";
}

@GetMapping("/dict/{id}")
public String dictionaryPlant(@PathVariable Long id, @AdminAuth Admin admin, Model model) {
if (admin == null) {
return REDIRECT_ADMIN_LOGIN;
}

DictionaryPlant dictionaryPlant = dictionaryPlantRepository.findById(id)
.orElseThrow(() -> new NoSuchElementException("일치하는 사전 식물이 존재하지 않습니다. id:" + id));

model.addAttribute("admin", admin);
model.addAttribute("plant", dictionaryPlant);
return "/admin/dict/plant";
}

@GetMapping("/dict/create")
public String dictionaryPlantCreateForm(@AdminAuth Admin admin, Model model) {
if (admin == null) {
return REDIRECT_ADMIN_LOGIN;
}

model.addAttribute("admin", admin);
return "/admin/dict/create";
}

@GetMapping("/dict/{id}/update")
public String dictionaryPlantUpdateForm(@PathVariable Long id, @AdminAuth Admin admin, Model model) {
if (admin == null) {
return REDIRECT_ADMIN_LOGIN;
}

DictionaryPlant dictionaryPlant = dictionaryPlantRepository.findById(id)
.orElseThrow(() -> new NoSuchElementException("일치하는 사전 식물이 존재하지 않습니다. id:" + id));

model.addAttribute("admin", admin);
model.addAttribute("plant", dictionaryPlant);
return "/admin/dict/update";
}

@GetMapping("/dict/requests")
public String dictionaryPlantRequests(@AdminAuth Admin admin, Model model) {
if (admin == null) {
return REDIRECT_ADMIN_LOGIN;
}

model.addAttribute("admin", admin);
return "/admin/dict/requests";
}

@GetMapping("/login")
public String loginPage(Model model) {
return "/admin/login";
}

@PostMapping("/login")
public String login(@RequestBody @Valid AdminLoginRequest admin, HttpSession httpSession) {
adminService.login(admin, httpSession);
return "redirect:/admin";
}

@PostMapping("/logout")
public ResponseEntity<Void> logout(HttpServletRequest request) {
HttpSession session = request.getSession(false);

if (session != null) {
session.invalidate();
}

return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
package com.official.pium.controller;


import com.official.pium.domain.Admin;
import com.official.pium.domain.AdminAuth;
import com.official.pium.service.DictionaryPlantService;
import com.official.pium.service.dto.DataResponse;
import com.official.pium.service.dto.DictionaryPlantCreateRequest;
import com.official.pium.service.dto.DictionaryPlantResponse;
import com.official.pium.service.dto.DictionaryPlantSearchResponse;
import com.official.pium.service.dto.DictionaryPlantUpdateRequest;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Positive;
import java.net.URI;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -38,4 +48,22 @@ public ResponseEntity<DataResponse<List<DictionaryPlantSearchResponse>>> searchD
DataResponse<List<DictionaryPlantSearchResponse>> dataResponse = dictionaryPlantService.search(name);
return ResponseEntity.ok(dataResponse);
}

@PostMapping
public ResponseEntity<Void> create(@AdminAuth Admin admin, @RequestBody @Valid DictionaryPlantCreateRequest request) {
Long dictionaryPlantId = dictionaryPlantService.create(request);
return ResponseEntity.created(URI.create("/dictionary-plants/" + dictionaryPlantId)).build();
}

@PatchMapping("/{id}")
public ResponseEntity<Void> update(@AdminAuth Admin admin, @PathVariable Long id, @RequestBody @Valid DictionaryPlantUpdateRequest request) {
dictionaryPlantService.update(id, request);
return ResponseEntity.ok().build();
}

@DeleteMapping("/{id}")
public ResponseEntity<Void> delete(@AdminAuth Admin admin, @PathVariable Long id) {
dictionaryPlantService.delete(id);
return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.official.pium.controller;

import com.official.pium.exception.AuthorizationException;
import com.official.pium.exception.OAuthException;
import com.official.pium.exception.OAuthException.KaKaoMemberInfoRequestException;
import com.official.pium.exception.OAuthException.KakaoServerException;
Expand Down Expand Up @@ -112,6 +113,14 @@ public ResponseEntity<GlobalExceptionResponse> handleConstraintViolationExceptio
return ResponseEntity.badRequest().body(exceptionResponse);
}

@ExceptionHandler(AuthorizationException.class)
public ResponseEntity<GlobalExceptionResponse> handleAuthorizationException(AuthorizationException e) {
String message = e.getMessage();
GlobalExceptionResponse exceptionResponse = createExceptionResponse(message);
log.warn(message);
return new ResponseEntity<>(exceptionResponse, HttpStatus.FORBIDDEN);
}

private StringBuilder getExceptionMessages(ConstraintViolationException e) {
Iterator<ConstraintViolation<?>> iterator =
e.getConstraintViolations().iterator();
Expand All @@ -131,4 +140,5 @@ private GlobalExceptionResponse createExceptionResponse(String message) {
.message(message)
.build();
}

}
20 changes: 20 additions & 0 deletions backend/pium/src/main/java/com/official/pium/domain/Admin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.official.pium.domain;

import java.util.Objects;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public class Admin {

private final String account;
private final String password;
private final String secondPassword;

public boolean isValidate(String account, String password, String secondPassword) {
return Objects.equals(this.account, account)
&& Objects.equals(this.password, password)
&& Objects.equals(this.secondPassword, secondPassword);
}
}
12 changes: 12 additions & 0 deletions backend/pium/src/main/java/com/official/pium/domain/AdminAuth.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.official.pium.domain;

import static java.lang.annotation.ElementType.PARAMETER;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface AdminAuth {
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,30 @@ private DictionaryPlant(String name, String imageUrl, String familyName, String
this.waterCycle = waterCycle;
}

public void updateDictionaryPlant(String name, String imageUrl, String familyName, String smell, String poison,
String manageLevel, String growSpeed, String requireTemp, String minimumTemp,
String requireHumidity, String postingPlace, String specialManageInfo,
String spring, String summer, String autumn, String winter) {
this.name = name;
this.imageUrl = imageUrl;
this.familyName = familyName;
this.smell = smell;
this.poison = poison;
this.manageLevel = manageLevel;
this.growSpeed = growSpeed;
this.requireTemp = requireTemp;
this.minimumTemp = minimumTemp;
this.requireHumidity = requireHumidity;
this.postingPlace = postingPlace;
this.specialManageInfo = specialManageInfo;
this.waterCycle = WaterCycle.builder()
.spring(spring)
.summer(summer)
.autumn(autumn)
.winter(winter)
.build();
}

@Override
public final boolean equals(Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.official.pium.exception;

public class AuthorizationException extends RuntimeException {

public AuthorizationException() {
super();
}

public AuthorizationException(final String message) {
super(message);
}

public static class NeedAdminException extends AuthorizationException {

public NeedAdminException() {
super();
}

public NeedAdminException(final String message) {
super(message);
}
}
}
Loading

0 comments on commit c93059d

Please sign in to comment.