Skip to content

Commit

Permalink
Merge pull request #11 from Timetris-Trendithon/mainpage
Browse files Browse the repository at this point in the history
⚑ Fix: μœ μ €μ•„μ΄λ”” νŒŒλΌλ―Έν„° 없이도 접속 κ°€λŠ₯ν•˜κ²Œ μˆ˜μ •, μΉ΄ν…Œκ³ λ¦¬ ν•„μš”ν•œ μ •λ³΄λ§Œ 리턴
  • Loading branch information
Zena0128 authored Feb 18, 2024
2 parents 5314ca2 + bfde2f9 commit afa60a2
Show file tree
Hide file tree
Showing 9 changed files with 259 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import com.trendithon.timetris.domain.mainpage.dto.CategoryRequestDTO;
import com.trendithon.timetris.domain.mainpage.dto.CategoryViewDTO;
import com.trendithon.timetris.domain.mainpage.service.CategoryService;
import com.trendithon.timetris.global.auth.jwt.TokenProvider;
import com.trendithon.timetris.global.exception.ApiResponse;
import com.trendithon.timetris.global.exception.CustomException;
import com.trendithon.timetris.global.exception.enums.ErrorStatus;
import com.trendithon.timetris.global.exception.enums.SuccessStatus;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
Expand All @@ -21,42 +23,95 @@
public class CategoryController {

private final CategoryService categoryService;
private final TokenProvider tokenProvider;

@GetMapping("/{userId}")
public ApiResponse<List<Category>> readCategoryAll(Authentication authentication,
@PathVariable long userId)
@GetMapping("")
public ApiResponse<List<CategoryViewDTO>> readCategoryAll(HttpServletRequest request)
{
List<Category> categoryList = categoryService.readCategoryAll(userId);
String accessToken = tokenProvider.extractAccessToken(request).orElse(null);

Long userId = null;

if (accessToken != null) {
userId = tokenProvider.extractId(accessToken).orElse(null);
} else {
return ApiResponse.failure(ErrorStatus.NOT_LOGIN_ERROR);

}
if(userId == null) {
throw new CustomException(ErrorStatus.USER_NOT_FOUND_ERROR);
}

List<CategoryViewDTO> categoryList = categoryService.readCategoryAll(userId);
return ApiResponse.success(SuccessStatus.OK, categoryList);
}

@PostMapping("")
public ApiResponse createCategory(Authentication authentication,
public ApiResponse<CategoryRequestDTO> createCategory(HttpServletRequest request,
@RequestBody CategoryRequestDTO categoryRequestDTO)
{
long userId = 1;
String accessToken = tokenProvider.extractAccessToken(request).orElse(null);

Long userId = null;

if (accessToken != null) {
userId = tokenProvider.extractId(accessToken).orElse(null);
} else {
return ApiResponse.failure(ErrorStatus.NOT_LOGIN_ERROR);

}
if(userId == null) {
throw new CustomException(ErrorStatus.USER_NOT_FOUND_ERROR);
}

Category category = categoryService.createCategory(userId, categoryRequestDTO);
if (category == null){
throw new CustomException(ErrorStatus.INVALID_FORMAT_ERROR);
}
return ApiResponse.success(SuccessStatus.OK);
return ApiResponse.success(SuccessStatus.OK, categoryRequestDTO);
}

@PutMapping("/{categoryId}")
public ApiResponse updateCategory(Authentication authentication,
public ApiResponse<CategoryViewDTO> updateCategory(HttpServletRequest request,
@PathVariable long categoryId,
@RequestBody CategoryViewDTO categoryViewDTO)
{
long userId = 1;
String accessToken = tokenProvider.extractAccessToken(request).orElse(null);

Long userId = null;

if (accessToken != null) {
userId = tokenProvider.extractId(accessToken).orElse(null);
} else {
return ApiResponse.failure(ErrorStatus.NOT_LOGIN_ERROR);

}
if(userId == null) {
throw new CustomException(ErrorStatus.USER_NOT_FOUND_ERROR);
}

categoryService.updateCategory(userId, categoryId, categoryViewDTO);
return ApiResponse.success(SuccessStatus.OK);
return ApiResponse.success(SuccessStatus.OK, categoryViewDTO);
}

@DeleteMapping("/{categoryId}")
public ApiResponse deleteCategory(Authentication authentication,
public ApiResponse deleteCategory(HttpServletRequest request,
@PathVariable long categoryId)
{
long userId = 1;
String accessToken = tokenProvider.extractAccessToken(request).orElse(null);

Long userId = null;

if (accessToken != null) {
userId = tokenProvider.extractId(accessToken).orElse(null);
} else {
return ApiResponse.failure(ErrorStatus.NOT_LOGIN_ERROR);

}
if(userId == null) {
throw new CustomException(ErrorStatus.USER_NOT_FOUND_ERROR);
}

categoryService.deleteCategory(userId, categoryId);
return ApiResponse.success(SuccessStatus.OK);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import com.trendithon.timetris.domain.mainpage.dto.DoRequestDTO;
import com.trendithon.timetris.domain.mainpage.dto.DoViewDTO;
import com.trendithon.timetris.domain.mainpage.service.DoService;
import com.trendithon.timetris.global.auth.jwt.TokenProvider;
import com.trendithon.timetris.global.exception.ApiResponse;
import com.trendithon.timetris.global.exception.CustomException;
import com.trendithon.timetris.global.exception.enums.ErrorStatus;
import com.trendithon.timetris.global.exception.enums.SuccessStatus;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
Expand All @@ -19,12 +21,26 @@
public class DoController {

private final DoService doService;
private final TokenProvider tokenProvider;

@PostMapping("")
public ApiResponse createDo(Authentication authentication,
public ApiResponse createDo(HttpServletRequest request,
@RequestBody DoRequestDTO doRequestDTO)
{
long userId = 1;
String accessToken = tokenProvider.extractAccessToken(request).orElse(null);

Long userId = null;

if (accessToken != null) {
userId = tokenProvider.extractId(accessToken).orElse(null);
} else {
return ApiResponse.failure(ErrorStatus.NOT_LOGIN_ERROR);

}
if(userId == null) {
throw new CustomException(ErrorStatus.USER_NOT_FOUND_ERROR);
}

Do done = doService.createDo(userId, doRequestDTO);
if (done == null){
throw new CustomException(ErrorStatus.INVALID_FORMAT_ERROR);
Expand All @@ -33,20 +49,46 @@ public ApiResponse createDo(Authentication authentication,
}

@PutMapping("/{doId}")
public ApiResponse updateDo(Authentication authentication,
public ApiResponse updateDo(HttpServletRequest request,
@PathVariable long doId,
@RequestBody DoViewDTO doViewDTO)
{
long userId = 1;
String accessToken = tokenProvider.extractAccessToken(request).orElse(null);

Long userId = null;

if (accessToken != null) {
userId = tokenProvider.extractId(accessToken).orElse(null);
} else {
return ApiResponse.failure(ErrorStatus.NOT_LOGIN_ERROR);

}
if(userId == null) {
throw new CustomException(ErrorStatus.USER_NOT_FOUND_ERROR);
}

doService.updateDo(userId, doId, doViewDTO);
return ApiResponse.success(SuccessStatus.OK);
}

@DeleteMapping("/{doId}")
public ApiResponse deleteDo(Authentication authentication,
public ApiResponse deleteDo(HttpServletRequest request,
@PathVariable long doId)
{
long userId = 1;
String accessToken = tokenProvider.extractAccessToken(request).orElse(null);

Long userId = null;

if (accessToken != null) {
userId = tokenProvider.extractId(accessToken).orElse(null);
} else {
return ApiResponse.failure(ErrorStatus.NOT_LOGIN_ERROR);

}
if(userId == null) {
throw new CustomException(ErrorStatus.USER_NOT_FOUND_ERROR);
}

doService.deleteDo(userId, doId);
return ApiResponse.success(SuccessStatus.OK);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@ public class MainPageController {
private final MainPageService mainPageService;
private final TokenProvider tokenProvider;

@GetMapping("/{userId}")
public ApiResponse<MainPageDTO> getMainPage(Authentication authentication,
@PathVariable long userId) {
MainPageDTO mainPageDTO = mainPageService.getMainPage(userId);
return ApiResponse.success(SuccessStatus.OK, mainPageDTO);
}
@GetMapping("")
public ApiResponse<MainPageDTO> getMainPage(HttpServletRequest request) {
String accessToken = tokenProvider.extractAccessToken(request).orElse(null);

Long userId = null;

@GetMapping
public ApiResponse<String> MainForTest(HttpServletRequest request) {
String userName = (String) request.getSession().getAttribute("name");

if (userName == null) {
return ApiResponse.of("LOGIN", "둜그인 ν•˜μ„Έμš”");
if (accessToken != null) {
userId = tokenProvider.extractId(accessToken).orElse(null);
} else {
return ApiResponse.success(SuccessStatus.OK, userName);
}
return ApiResponse.failure(ErrorStatus.NOT_LOGIN_ERROR);

}
if(userId == null) {
throw new CustomException(ErrorStatus.USER_NOT_FOUND_ERROR);
}
MainPageDTO mainPageDTO = mainPageService.getMainPage(userId);
return ApiResponse.success(SuccessStatus.OK, mainPageDTO);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
import com.trendithon.timetris.domain.mainpage.dto.PlanRequestDTO;
import com.trendithon.timetris.domain.mainpage.dto.PlanViewDTO;
import com.trendithon.timetris.domain.mainpage.service.PlanService;
import com.trendithon.timetris.domain.member.domain.User;
import com.trendithon.timetris.global.auth.jwt.TokenProvider;
import com.trendithon.timetris.global.exception.ApiResponse;
import com.trendithon.timetris.global.exception.CustomException;
import com.trendithon.timetris.global.exception.enums.ErrorStatus;
import com.trendithon.timetris.global.exception.enums.SuccessStatus;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
Expand All @@ -20,12 +23,26 @@
public class PlanController {

private final PlanService planService;
private final TokenProvider tokenProvider;

@PostMapping("")
public ApiResponse createPlan(Authentication authentication,
public ApiResponse createPlan(HttpServletRequest request,
@RequestBody PlanRequestDTO planRequestDTO)
{
long userId = 1;
String accessToken = tokenProvider.extractAccessToken(request).orElse(null);

Long userId = null;

if (accessToken != null) {
userId = tokenProvider.extractId(accessToken).orElse(null);
} else {
return ApiResponse.failure(ErrorStatus.NOT_LOGIN_ERROR);

}
if(userId == null) {
throw new CustomException(ErrorStatus.USER_NOT_FOUND_ERROR);
}

Plan plan = planService.createPlan(userId, planRequestDTO);
if (plan == null){
throw new CustomException(ErrorStatus.INVALID_FORMAT_ERROR);
Expand All @@ -34,29 +51,68 @@ public ApiResponse createPlan(Authentication authentication,
}

@PutMapping("/{planId}")
public ApiResponse updatePlan(Authentication authentication,
public ApiResponse updatePlan(HttpServletRequest request,
@PathVariable long planId,
@RequestBody PlanViewDTO planViewDTO)
{
long userId = 1;
String accessToken = tokenProvider.extractAccessToken(request).orElse(null);

Long userId = null;

if (accessToken != null) {
userId = tokenProvider.extractId(accessToken).orElse(null);
} else {
return ApiResponse.failure(ErrorStatus.NOT_LOGIN_ERROR);

}
if(userId == null) {
throw new CustomException(ErrorStatus.USER_NOT_FOUND_ERROR);
}

planService.updatePlan(userId, planId, planViewDTO);
return ApiResponse.success(SuccessStatus.OK);
}

@DeleteMapping("/{planId}")
public ApiResponse deletePlan(Authentication authentication,
public ApiResponse deletePlan(HttpServletRequest request,
@PathVariable long planId)
{
long userId = 1;
String accessToken = tokenProvider.extractAccessToken(request).orElse(null);

Long userId = null;

if (accessToken != null) {
userId = tokenProvider.extractId(accessToken).orElse(null);
} else {
return ApiResponse.failure(ErrorStatus.NOT_LOGIN_ERROR);

}
if(userId == null) {
throw new CustomException(ErrorStatus.USER_NOT_FOUND_ERROR);
}

planService.deletePlan(userId, planId);
return ApiResponse.success(SuccessStatus.OK);
}

@PutMapping("/{planId}/check")
public ApiResponse donePlan(Authentication authentication,
public ApiResponse donePlan(HttpServletRequest request,
@PathVariable long planId)
{
long userId = 1;
String accessToken = tokenProvider.extractAccessToken(request).orElse(null);

Long userId = null;

if (accessToken != null) {
userId = tokenProvider.extractId(accessToken).orElse(null);
} else {
return ApiResponse.failure(ErrorStatus.NOT_LOGIN_ERROR);

}
if(userId == null) {
throw new CustomException(ErrorStatus.USER_NOT_FOUND_ERROR);
}

planService.donePlan(userId, planId);
return ApiResponse.success(SuccessStatus.OK);
}
Expand Down
Loading

0 comments on commit afa60a2

Please sign in to comment.