Skip to content

Commit

Permalink
add required args constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhamv108 committed Jan 23, 2024
1 parent a92e67a commit 2fd4211
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import code.shubham.commons.treecommons.ITreeService;
import code.shubham.commons.treemodels.TreeNodeDTO;
import code.shubham.commons.treemodels.TreePathDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
Expand All @@ -14,15 +15,11 @@

@Slf4j
@Service
@RequiredArgsConstructor
public class TreeService implements ITreeService {

private final TreeRepository repository;

@Autowired
public TreeService(TreeRepository repository) {
this.repository = repository;
}

@Override
public Tree save(final TreeNodeDTO node, final Integer parentId) {
final Tree created = this.repository.save(Tree.builder().title(node.getTitle()).parentId(parentId).build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import code.shubham.core.blobstore.dao.entities.Blob;
import code.shubham.core.blobstore.dao.repositories.BlobRepository;
import code.shubham.core.blobstoremodels.CreateBlobResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -13,18 +14,14 @@

@Slf4j
@Service
@RequiredArgsConstructor
public class BlobService {

private final BlobRepository repository;

@Value("${aws.default.region}")
private String defaultRegion;

@Autowired
public BlobService(final BlobRepository repository) {
this.repository = repository;
}

public CreateBlobResponse getPreSignedUploadUrl(final String owner, final String bucket, final String key,
final Map<String, String> metadata) {
final Blob blob = this.repository.save(Blob.builder().bucket(bucket).keyName(key).owner(owner).build());
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/code/shubham/core/iam/services/RoleService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,24 @@
import code.shubham.core.iam.dao.entities.Role;
import code.shubham.core.iam.dao.repositories.RoleRepository;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@Slf4j
@Service
@RequiredArgsConstructor
public class RoleService {

private final RoleRepository repository;

private final Map<String, Role> cache = new HashMap<>();

@Autowired
public RoleService(RoleRepository repository) {
this.repository = repository;
}

@PostConstruct
public void init() {
this.repository.findAnyTen().stream().forEach(role -> this.cache.put(role.getName(), role));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,23 @@
import code.shubham.commons.exceptions.InvalidParameterException;
import code.shubham.core.iam.dao.entities.UserRole;
import code.shubham.core.iam.dao.repositories.UserRoleRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
public class UserRoleService {

private final UserRoleRepository repository;

private final RoleService roleService;

@Autowired
public UserRoleService(final UserRoleRepository repository, final RoleService roleService) {
this.repository = repository;
this.roleService = roleService;
}

public UserRole setRoleToUser(final String roleName, final Long userId) {
return this.roleService.getRoleByName(roleName)
.map(role -> this.repository.save(UserRole.builder().role(role).userId(userId).build()))
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/code/shubham/core/iam/services/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,24 @@
import code.shubham.core.iammodels.GetOrCreateUser;
import code.shubham.core.iammodels.GetUserResponse;
import code.shubham.core.iammodels.UserDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Slf4j
@Transactional
@Service
@RequiredArgsConstructor
public class UserService implements IUserService {

private final UserRepository repository;

private final UserRoleService userRoleService;

@Autowired
public UserService(final UserRepository repository, final UserRoleService userRoleService) {
this.repository = repository;
this.userRoleService = userRoleService;
}

@Override
public GetUserResponse getOrCreate(final GetOrCreateUser.Request request) {
final Optional<User> existing = this.repository.findByEmail(request.getEmail());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import code.shubham.core.iammodels.GetOrCreateUser;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.http.HttpStatus;
Expand All @@ -16,15 +17,11 @@
@SecurityRequirement(name = "BearerAuth")
@Tag(name = "User")
@ConditionalOnProperty(prefix = "service", name = "module", havingValue = "web")
@RequiredArgsConstructor
public class UserController {

private final UserService userService;

@Autowired
public UserController(final UserService userService) {
this.userService = userService;
}

@GetMapping
public ResponseEntity<?> getOrCreateUser(@RequestBody GetOrCreateUser.Request request) {
return ResponseUtils.getDataResponseEntity(HttpStatus.OK, this.userService.getOrCreate(request));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@
import code.shubham.commons.generators.id.implementations.SnowflakeSequenceIdGenerator;
import code.shubham.core.lock.dao.entites.Lock;
import code.shubham.core.lock.dao.repositories.LockRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Slf4j
@Service
@RequiredArgsConstructor
public class LockService {

private final LockRepository repository;

@Autowired
public LockService(final LockRepository repository) {
this.repository = repository;
}

public boolean lock(final String name, final int previousVersion, final String owner,
final long timeToLiveInSeconds) {
if (previousVersion == 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.http.HttpStatus;
Expand All @@ -33,15 +34,11 @@
@SecurityRequirement(name = "BearerAuth")
@Tag(name = "Lock")
@ConditionalOnProperty(prefix = "service", name = "module", havingValue = "web")
@RequiredArgsConstructor
public class LockController {

private final LockService service;

@Autowired
public LockController(final LockService service) {
this.service = service;
}

@GetMapping("/{name}")
public ResponseEntity<?> getByName(@PathVariable @NotNull @NotEmpty final String name) {
this.validateNameOrThrowException(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@
import code.shubham.commons.exceptions.InvalidRequestException;
import code.shubham.core.userprofile.dao.entities.UserProfile;
import code.shubham.core.userprofile.dao.repositories.UserProfileRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class UserProfileService {

private final UserProfileRepository repository;

@Autowired
public UserProfileService(final UserProfileRepository repository) {
this.repository = repository;
}

public UserProfile update(final Long userId, final String address) {
return this.repository.save(this.repository.findByUserId(userId).map(profile -> {
profile.setAddress(address);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.http.HttpStatus;
Expand All @@ -21,15 +22,11 @@
@SecurityRequirement(name = "BearerAuth")
@Tag(name = "User Profiles")
@ConditionalOnProperty(prefix = "service", name = "module", havingValue = "web")
@RequiredArgsConstructor
public class UserProfileController {

private final UserProfileService service;

@Autowired
public UserProfileController(final UserProfileService service) {
this.service = service;
}

@Operation(description = "PUT Profile Information",
summary = "Idempotent endpoint for setting user profile information like address etc",
responses = { @ApiResponse(description = "Success", responseCode = "200"),
Expand Down

0 comments on commit 2fd4211

Please sign in to comment.