Skip to content

Commit

Permalink
#1 - Feat: Add User Classes
Browse files Browse the repository at this point in the history
  • Loading branch information
umtuk committed Jul 15, 2023
1 parent d2ee8e1 commit f7b8b52
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.routemaster.api.auth.endpoint.user.impl.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.routemaster.api.auth.domain.user.jwt.impl.data.UserJwtPayload;
import org.routemaster.api.auth.domain.user.jwt.impl.utils.filter.UserJwtAuthenticationFilter;
import org.routemaster.api.auth.endpoint.user.impl.service.UserEndpointService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserRestController {

private final UserEndpointService userEndpointService;

@PatchMapping("/logout")
public ResponseEntity<Void> logout(
@RequestAttribute(UserJwtAuthenticationFilter.USER_PAYLOAD) UserJwtPayload payload) {
userEndpointService.logout(payload);
return new ResponseEntity<>(HttpStatus.OK);
}

@DeleteMapping
public ResponseEntity<Void> delete(
@RequestAttribute(UserJwtAuthenticationFilter.USER_PAYLOAD) UserJwtPayload payload) {
userEndpointService.delete(payload);
return new ResponseEntity<>(HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.routemaster.api.auth.endpoint.user.impl.service;

import org.routemaster.api.auth.domain.user.jwt.impl.data.UserJwtPayload;

public interface UserEndpointService {

void logout(UserJwtPayload payload);
void delete(UserJwtPayload payload);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package org.routemaster.api.auth.endpoint.user.impl.service.impl;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.routemaster.api.auth.domain.user.email.impl.service.EmailUserService;
import org.routemaster.api.auth.domain.user.impl.data.BaseUser;
import org.routemaster.api.auth.domain.user.impl.exception.UserErrorCode;
import org.routemaster.api.auth.domain.user.impl.service.BaseUserService;
import org.routemaster.api.auth.domain.user.jwt.impl.data.UserJwtPayload;
import org.routemaster.api.auth.domain.user.jwt.impl.exception.UserJwtErrorDescription;
import org.routemaster.api.auth.domain.user.social.impl.service.SocialUserService;
import org.routemaster.api.auth.endpoint.user.impl.service.UserEndpointService;
import org.routemaster.sdk.exception.data.roe.ROEFactory;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class DefaultUserEndpointService implements UserEndpointService {

private final BaseUserService baseUserService;
private final EmailUserService emailUserService;
private final SocialUserService socialUserService;
private final ROEFactory roeFactory;

@Override
public void logout(UserJwtPayload payload) {
switch (payload.getUserType()) {
case EMAIL_USER:
emailUserService.updateRefreshToken(payload.getTypeUserId(), null);
break;
case SOCIAL_USER:
socialUserService.updateRefreshToken(payload.getTypeUserId(), null);
break;
default: throw roeFactory.get(
UserErrorCode.ROE_102,
UserJwtErrorDescription.INVALID_TOKEN,
HttpStatus.UNAUTHORIZED
);
}
}

@Override
public void delete(UserJwtPayload payload) {
BaseUser baseUser = baseUserService.details(payload.getBaseUserId());
switch (payload.getUserType()) {
case EMAIL_USER:
emailUserService.delete(payload.getTypeUserId());
baseUser.getEmailUsers().remove(payload.getTypeUserId());
break;
case SOCIAL_USER:
socialUserService.delete(payload.getTypeUserId());
baseUser.getSocialUsers().remove(payload.getTypeUserId());
break;
default: throw roeFactory.get(
UserErrorCode.ROE_102,
UserJwtErrorDescription.INVALID_TOKEN,
HttpStatus.UNAUTHORIZED
);
}

if (baseUser.getEmailUsers().isEmpty() && baseUser.getSocialUsers().isEmpty()) {
baseUserService.delete(baseUser.getId());
}
else {
baseUserService.save(baseUser);
}
}
}

0 comments on commit f7b8b52

Please sign in to comment.