Skip to content

Commit

Permalink
#1 - Feat: Add User Privacy Classes
Browse files Browse the repository at this point in the history
  • Loading branch information
umtuk committed Jul 15, 2023
1 parent dd85f0e commit 62ae0ed
Show file tree
Hide file tree
Showing 15 changed files with 559 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.routemaster.api.auth.domain.user.info.privacy.impl.data;

import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import java.time.LocalDateTime;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;

@Document(collection = "user_privacy")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@Builder
@Getter
@ToString
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class UserPrivacy {

@Id
private String id;
@Field(name = "privacy_group_id")
private String privacyGroupId;
@Field(name = "base_user_id")
private String baseUserId;
@Field(name = "is_approved")
private Boolean isApproved;
@Field(name = "created_at")
private LocalDateTime createdAt;
@Field(name = "updated_at")
private LocalDateTime updatedAt;
@Field(name = "expired_at")
private LocalDateTime expiredAt;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.routemaster.api.auth.domain.user.info.privacy.impl.persistence;

import java.util.List;
import java.util.Optional;
import java.util.Set;
import org.routemaster.api.auth.domain.user.info.privacy.impl.data.UserPrivacy;
import org.springframework.data.mongodb.repository.MongoRepository;

public interface UserPrivacyRepository extends MongoRepository<UserPrivacy, String> {

Optional<UserPrivacy> findByBaseUserIdAndPrivacyGroupId(String baseUserId, String privacyGroupId);
List<UserPrivacy> findAllByBaseUserId(String baseUserId);
Boolean existsAllById(Iterable<String> ids);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.routemaster.api.auth.domain.user.info.privacy.impl.service;

import java.util.List;
import org.routemaster.api.auth.domain.user.info.privacy.impl.data.UserPrivacy;

public interface UserPrivacyService {

UserPrivacy details(String id);
UserPrivacy details(String baseUserId, String privacyGroupId);
List<UserPrivacy> list(String baseUserId);
UserPrivacy save(UserPrivacy userPrivacy);
List<UserPrivacy> saveAll(Iterable<UserPrivacy> userPrivacies);
void delete(String id);
void deleteAll(Iterable<String> ids);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package org.routemaster.api.auth.domain.user.info.privacy.impl.service.impl;

import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.routemaster.api.auth.domain.user.info.impl.exception.InfoErrorCode;
import org.routemaster.api.auth.domain.user.info.privacy.impl.data.UserPrivacy;
import org.routemaster.api.auth.domain.user.info.privacy.impl.exception.PrivacyErrorDescription;
import org.routemaster.api.auth.domain.user.info.privacy.impl.persistence.UserPrivacyRepository;
import org.routemaster.api.auth.domain.user.info.privacy.impl.service.UserPrivacyService;
import org.routemaster.sdk.exception.data.roe.ROEFactory;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class DefaultUserPrivacyService implements UserPrivacyService {

private final UserPrivacyRepository userPrivacyRepository;

private final ROEFactory roeFactory;

@Override
public UserPrivacy details(String id) {
return userPrivacyRepository.findById(id)
.orElseThrow(() -> roeFactory.get(
InfoErrorCode.ROE_111,
PrivacyErrorDescription.USER_PRIVACY_NOT_FOUND,
HttpStatus.NOT_FOUND
));
}

@Override
public UserPrivacy details(String baseUserId, String privacyGroupId) {
return userPrivacyRepository.findByBaseUserIdAndPrivacyGroupId(baseUserId, privacyGroupId)
.orElseThrow(() -> roeFactory.get(
InfoErrorCode.ROE_111,
PrivacyErrorDescription.USER_PRIVACY_NOT_FOUND,
HttpStatus.NOT_FOUND
));
}

@Override
public List<UserPrivacy> list(String baseUserId) {
return userPrivacyRepository.findAllByBaseUserId(baseUserId);
}

@Override
public UserPrivacy save(UserPrivacy userPrivacy) {
return userPrivacyRepository.save(userPrivacy);
}

@Override
public List<UserPrivacy> saveAll(Iterable<UserPrivacy> userPrivacies) {
return userPrivacyRepository.saveAll(userPrivacies);
}

@Override
public void delete(String id) {
if (!userPrivacyRepository.existsById(id)) {
throw roeFactory.get(
InfoErrorCode.ROE_111,
PrivacyErrorDescription.USER_PRIVACY_NOT_FOUND,
HttpStatus.NOT_FOUND
);
}
userPrivacyRepository.deleteById(id);
}

@Override
public void deleteAll(Iterable<String> ids) {
if (!userPrivacyRepository.existsAllById(ids)) {
throw roeFactory.get(
InfoErrorCode.ROE_111,
PrivacyErrorDescription.USER_PRIVACY_NOT_FOUND,
HttpStatus.NOT_FOUND
);
}
userPrivacyRepository.deleteAllById(ids);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package org.routemaster.api.auth.endpoint.user.info.privacy.impl.controller;

import java.util.List;
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.info.privacy.impl.service.UserPrivacyEndpointService;
import org.routemaster.api.auth.endpoint.user.info.privacy.impl.vo.request.UserPrivacySaveAllRequest;
import org.routemaster.api.auth.endpoint.user.info.privacy.impl.vo.request.UserPrivacySaveRequest;
import org.routemaster.api.auth.endpoint.user.info.privacy.impl.vo.response.UserPrivacyDeleteResponse;
import org.routemaster.api.auth.endpoint.user.info.privacy.impl.vo.response.UserPrivacyDetailsResponse;
import org.routemaster.api.auth.endpoint.user.info.privacy.impl.vo.response.UserPrivacyListResponse;
import org.routemaster.api.auth.endpoint.user.info.privacy.impl.vo.response.UserPrivacySaveResponse;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
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.RequestAttribute;
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;

@Slf4j
@RestController
@RequestMapping("/user/info/privacy")
@RequiredArgsConstructor
public class UserPrivacyRestController {

private final UserPrivacyEndpointService userPrivacyEndpointService;

@GetMapping("/me")
@PreAuthorize("hasRole('ROLE_USER')")
public ResponseEntity<UserPrivacyListResponse> details(
@RequestAttribute(UserJwtAuthenticationFilter.USER_PAYLOAD) UserJwtPayload payload) {
UserPrivacyListResponse response = userPrivacyEndpointService.details(payload);
return new ResponseEntity<>(response, HttpStatus.OK);
}

@PostMapping
@PreAuthorize("hasRole('ROLE_USER')")
public ResponseEntity<UserPrivacySaveResponse> save(
@RequestAttribute(UserJwtAuthenticationFilter.USER_PAYLOAD) UserJwtPayload payload,
@RequestBody UserPrivacySaveRequest request) {
UserPrivacySaveResponse response = userPrivacyEndpointService.save(payload, request);
return new ResponseEntity<>(response, HttpStatus.OK);
}

@PostMapping("/list")
@PreAuthorize("hasRole('ROLE_USER')")
public ResponseEntity<UserPrivacySaveResponse> saveAll(
@RequestAttribute(UserJwtAuthenticationFilter.USER_PAYLOAD) UserJwtPayload payload,
@RequestBody UserPrivacySaveAllRequest request) {
UserPrivacySaveResponse response = userPrivacyEndpointService.saveAll(payload, request);
return new ResponseEntity<>(response, HttpStatus.OK);
}

@DeleteMapping("/{id}")
@PreAuthorize("hasRole('ROLE_USER')")
public ResponseEntity<UserPrivacyDeleteResponse> delete(@PathVariable String id,
@RequestAttribute(UserJwtAuthenticationFilter.USER_PAYLOAD) UserJwtPayload payload) {
UserPrivacyDeleteResponse response = userPrivacyEndpointService.delete(id, payload);
return new ResponseEntity<>(response, HttpStatus.OK);
}

@DeleteMapping("/list")
@PreAuthorize("hasRole('ROLE_USER')")
public ResponseEntity<UserPrivacyDeleteResponse> deleteAll(@RequestParam List<String> ids,
@RequestAttribute(UserJwtAuthenticationFilter.USER_PAYLOAD) UserJwtPayload payload) {
UserPrivacyDeleteResponse response = userPrivacyEndpointService.deleteAll(ids, payload);
return new ResponseEntity<>(response, HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.routemaster.api.auth.endpoint.user.info.privacy.impl.service;

import java.util.List;
import org.routemaster.api.auth.domain.user.jwt.impl.data.UserJwtPayload;
import org.routemaster.api.auth.endpoint.user.info.privacy.impl.vo.request.UserPrivacySaveAllRequest;
import org.routemaster.api.auth.endpoint.user.info.privacy.impl.vo.request.UserPrivacySaveRequest;
import org.routemaster.api.auth.endpoint.user.info.privacy.impl.vo.response.UserPrivacyDeleteResponse;
import org.routemaster.api.auth.endpoint.user.info.privacy.impl.vo.response.UserPrivacyDetailsResponse;
import org.routemaster.api.auth.endpoint.user.info.privacy.impl.vo.response.UserPrivacyListResponse;
import org.routemaster.api.auth.endpoint.user.info.privacy.impl.vo.response.UserPrivacySaveResponse;

public interface UserPrivacyEndpointService {


UserPrivacyListResponse details(UserJwtPayload payload);
UserPrivacySaveResponse save(UserJwtPayload payload, UserPrivacySaveRequest request);
UserPrivacySaveResponse saveAll(UserJwtPayload payload, UserPrivacySaveAllRequest request);
UserPrivacyDeleteResponse delete(String id, UserJwtPayload payload);
UserPrivacyDeleteResponse deleteAll(List<String> ids, UserJwtPayload payload);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package org.routemaster.api.auth.endpoint.user.info.privacy.impl.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.routemaster.api.auth.domain.user.info.privacy.impl.data.PrivacyGroup;
import org.routemaster.api.auth.domain.user.info.privacy.impl.data.UserPrivacy;
import org.routemaster.api.auth.domain.user.info.privacy.impl.service.PrivacyGroupService;
import org.routemaster.api.auth.domain.user.info.privacy.impl.service.UserPrivacyService;
import org.routemaster.api.auth.domain.user.jwt.impl.data.UserJwtPayload;
import org.routemaster.api.auth.endpoint.privacy.impl.service.PrivacyEndpointService;
import org.routemaster.api.auth.endpoint.privacy.impl.vo.response.PrivacyGroupListResponse;
import org.routemaster.api.auth.endpoint.privacy.impl.vo.response.PrivacyGroupResponse;
import org.routemaster.api.auth.endpoint.user.info.privacy.impl.service.UserPrivacyEndpointService;
import org.routemaster.api.auth.endpoint.user.info.privacy.impl.util.mapper.UserPrivacyMapper;
import org.routemaster.api.auth.endpoint.user.info.privacy.impl.util.mapper.UserPrivacyVOMapper;
import org.routemaster.api.auth.endpoint.user.info.privacy.impl.vo.request.UserPrivacySaveAllRequest;
import org.routemaster.api.auth.endpoint.user.info.privacy.impl.vo.request.UserPrivacySaveRequest;
import org.routemaster.api.auth.endpoint.user.info.privacy.impl.vo.response.UserPrivacyDeleteResponse;
import org.routemaster.api.auth.endpoint.user.info.privacy.impl.vo.response.UserPrivacyDetailsResponse;
import org.routemaster.api.auth.endpoint.user.info.privacy.impl.vo.response.UserPrivacyListResponse;
import org.routemaster.api.auth.endpoint.user.info.privacy.impl.vo.response.UserPrivacySaveResponse;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
@RequiredArgsConstructor
public class DefaultUserPrivacyEndpointService implements UserPrivacyEndpointService {

private final PrivacyEndpointService privacyEndpointService;
private final UserPrivacyService userPrivacyService;
private final UserPrivacyVOMapper userPrivacyVOMapper;
private final UserPrivacyMapper userPrivacyMapper;
private final PrivacyGroupService privacyGroupService;

@Override
public UserPrivacyListResponse details(UserJwtPayload payload) {
List<UserPrivacy> userPrivacyList = userPrivacyService.list(payload.getBaseUserId());
PrivacyGroupListResponse response = privacyEndpointService.all();
Map<String, PrivacyGroupResponse> privacyGroupMap = new HashMap<>();
for (PrivacyGroupResponse privacyGroup : response.getPrivacyGroups()) {
privacyGroupMap.put(privacyGroup.getId(), privacyGroup);
}
List<UserPrivacyDetailsResponse> responses =
userPrivacyVOMapper.list(userPrivacyList, privacyGroupMap);
return UserPrivacyListResponse.builder()
.userPrivacies(responses)
.build();
}

@Override
@Transactional
public UserPrivacySaveResponse save(UserJwtPayload payload, UserPrivacySaveRequest request) {
UserPrivacy prev;
try {
prev = userPrivacyService.details(payload.getBaseUserId(), request.getPrivacyGroupId());
} catch (Exception e) {
prev = null;
}
PrivacyGroup privacyGroup = privacyGroupService.details(request.getPrivacyGroupId());

UserPrivacy userPrivacy = prev == null ?
userPrivacyMapper.save(payload, request, privacyGroup) :
userPrivacyMapper.save(prev, request, privacyGroup);
userPrivacyService.save(userPrivacy);

UserPrivacyListResponse response = details(payload);
return UserPrivacySaveResponse.builder()
.userPrivacies(response.getUserPrivacies())
.build();
}

@Override
@Transactional
public UserPrivacySaveResponse saveAll(UserJwtPayload payload,
UserPrivacySaveAllRequest request) {
List<UserPrivacy> userPrivacies = userPrivacyService.list(payload.getBaseUserId());
Map<String, UserPrivacy> userPrivacyMap = new HashMap<>();
for (UserPrivacy userPrivacy : userPrivacies) {
userPrivacyMap.put(userPrivacy.getPrivacyGroupId(), userPrivacy);
}

List<PrivacyGroup> privacyGroups = privacyGroupService.currentPrivacyGroups();
Map<String, PrivacyGroup> privacyGroupMap = new HashMap<>();
for (PrivacyGroup privacyGroup : privacyGroups) {
privacyGroupMap.put(privacyGroup.getId(), privacyGroup);
}

List<UserPrivacy> saves = new ArrayList<>();
for (UserPrivacySaveRequest privacy : request.getPrivacies()) {
String privacyGroupId = privacy.getPrivacyGroupId();
if (userPrivacyMap.containsKey(privacyGroupId)) {
saves.add(userPrivacyMapper.save(
userPrivacyMap.get(privacyGroupId), privacy, privacyGroupMap.get(privacyGroupId)));
}
else {
saves.add(userPrivacyMapper.save(
payload, privacy, privacyGroupMap.get(privacyGroupId)));
}
}

userPrivacyService.saveAll(saves);
UserPrivacyListResponse response = details(payload);
return UserPrivacySaveResponse.builder()
.userPrivacies(response.getUserPrivacies())
.build();
}

@Override
@Transactional
public UserPrivacyDeleteResponse delete(String id, UserJwtPayload payload) {
userPrivacyService.delete(id);
UserPrivacyListResponse response = details(payload);
return UserPrivacyDeleteResponse.builder()
.userPrivacies(response.getUserPrivacies())
.build();
}

@Override
@Transactional
public UserPrivacyDeleteResponse deleteAll(List<String> ids, UserJwtPayload payload) {
userPrivacyService.deleteAll(ids);
UserPrivacyListResponse response = details(payload);
return UserPrivacyDeleteResponse.builder()
.userPrivacies(response.getUserPrivacies())
.build();
}
}
Loading

0 comments on commit 62ae0ed

Please sign in to comment.