Skip to content

Commit 0b82c7b

Browse files
committed
fix: lint
1 parent a3cd247 commit 0b82c7b

7 files changed

Lines changed: 72 additions & 60 deletions

File tree

‎build.gradle‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ checkstyle {
4949
}
5050

5151
spotbugs {
52-
toolVersion = '4.8.6'
52+
toolVersion = '4.9.8'
5353
effort = 'max'
5454
reportLevel = 'medium'
55+
excludeFilter = file('spotbugs-exclude.xml')
5556
}
5657

5758
tasks.withType(SpotBugsTask).configureEach {

‎spotbugs-exclude.xml‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<FindBugsFilter>
3+
<Match>
4+
<Class name="~com\.syncapi\.entity\..*"/>
5+
<Bug pattern="EI_EXPOSE_REP,EI_EXPOSE_REP2"/>
6+
</Match>
7+
8+
<Match>
9+
<Class name="~com\.syncapi\.dto\..*"/>
10+
<Bug pattern="EI_EXPOSE_REP,EI_EXPOSE_REP2"/>
11+
</Match>
12+
</FindBugsFilter>

‎src/main/java/com/syncapi/controller/workspace/WorkspaceController.java‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import com.syncapi.dto.workspace.WorkspaceRequest;
55
import com.syncapi.dto.workspace.WorkspaceResponse;
66
import com.syncapi.service.workspace.WorkspaceService;
7-
import com.syncapi.util.AuthUtil;
7+
import com.syncapi.util.Util;
88
import jakarta.validation.Valid;
99
import org.springframework.beans.factory.annotation.Autowired;
1010
import org.springframework.http.HttpStatus;
@@ -28,15 +28,15 @@ public class WorkspaceController {
2828

2929
@GetMapping
3030
public ResponseEntity<List<WorkspaceResponse>> getUserWorkspaces() {
31-
List<WorkspaceResponse> workspaces = workspaceService.getUserWorkspaces(AuthUtil.getCurrentUserEmail());
31+
List<WorkspaceResponse> workspaces = workspaceService.getUserWorkspaces(Util.getCurrentUserEmail());
3232

3333
return ResponseEntity.ok(workspaces);
3434
}
3535

3636
@GetMapping("/{id}")
3737
public ResponseEntity<WorkspaceResponse> getWorkspace(@PathVariable Long id) {
3838
try {
39-
WorkspaceResponse workspace = workspaceService.getWorkspace(id, AuthUtil.getCurrentUserEmail());
39+
WorkspaceResponse workspace = workspaceService.getWorkspace(id, Util.getCurrentUserEmail());
4040

4141
return ResponseEntity.ok(workspace);
4242
} catch (RuntimeException e) {
@@ -46,7 +46,7 @@ public ResponseEntity<WorkspaceResponse> getWorkspace(@PathVariable Long id) {
4646

4747
@PostMapping
4848
public ResponseEntity<WorkspaceResponse> createWorkspace(@Valid @RequestBody WorkspaceRequest request) {
49-
WorkspaceResponse workspace = workspaceService.createWorkspace(request, AuthUtil.getCurrentUserEmail());
49+
WorkspaceResponse workspace = workspaceService.createWorkspace(request, Util.getCurrentUserEmail());
5050

5151
return ResponseEntity.status(HttpStatus.CREATED).body(workspace);
5252
}
@@ -56,7 +56,7 @@ public ResponseEntity<WorkspaceResponse> createWorkspace(@Valid @RequestBody Wor
5656
public ResponseEntity<WorkspaceResponse> updateWorkspace(@PathVariable Long id,
5757
@Valid @RequestBody WorkspaceRequest request) {
5858
try {
59-
WorkspaceResponse workspace = workspaceService.updateWorkspace(id, request, AuthUtil.getCurrentUserEmail());
59+
WorkspaceResponse workspace = workspaceService.updateWorkspace(id, request, Util.getCurrentUserEmail());
6060

6161
return ResponseEntity.ok(workspace);
6262
} catch (RuntimeException e) {
@@ -67,7 +67,7 @@ public ResponseEntity<WorkspaceResponse> updateWorkspace(@PathVariable Long id,
6767
@DeleteMapping("/{id}")
6868
public ResponseEntity<Void> deleteWorkspace(@PathVariable Long id) {
6969
try {
70-
workspaceService.deleteWorkspace(id, AuthUtil.getCurrentUserEmail());
70+
workspaceService.deleteWorkspace(id, Util.getCurrentUserEmail());
7171

7272
return ResponseEntity.noContent().build();
7373
} catch (RuntimeException e) {
@@ -79,7 +79,7 @@ public ResponseEntity<Void> deleteWorkspace(@PathVariable Long id) {
7979
public ResponseEntity<WorkspaceResponse> addMember(@PathVariable Long id,
8080
@Valid @RequestBody AddMemberRequest request) {
8181
try {
82-
WorkspaceResponse workspace = workspaceService.addMember(id, request, AuthUtil.getCurrentUserEmail());
82+
WorkspaceResponse workspace = workspaceService.addMember(id, request, Util.getCurrentUserEmail());
8383

8484
return ResponseEntity.ok(workspace);
8585
} catch (RuntimeException e) {
@@ -90,7 +90,7 @@ public ResponseEntity<WorkspaceResponse> addMember(@PathVariable Long id,
9090
@DeleteMapping("/{id}/members/{userId}")
9191
public ResponseEntity<WorkspaceResponse> removeMember(@PathVariable Long id, @PathVariable Long userId) {
9292
try {
93-
WorkspaceResponse workspace = workspaceService.removeMember(id, userId, AuthUtil.getCurrentUserEmail());
93+
WorkspaceResponse workspace = workspaceService.removeMember(id, userId, Util.getCurrentUserEmail());
9494

9595
return ResponseEntity.ok(workspace);
9696
} catch (RuntimeException e) {

‎src/main/java/com/syncapi/service/workspace/WorkspaceService.java‎

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import com.syncapi.entity.User;
77
import com.syncapi.entity.Workspace;
88
import com.syncapi.repository.WorkspaceRepository;
9-
import com.syncapi.util.UserUtil;
9+
import com.syncapi.util.Util;
1010
import jakarta.transaction.Transactional;
1111
import org.springframework.beans.factory.annotation.Autowired;
1212
import org.springframework.stereotype.Service;
@@ -19,8 +19,11 @@ public class WorkspaceService {
1919
@Autowired
2020
private WorkspaceRepository workspaceRepository;
2121

22+
@Autowired
23+
private Util util;
24+
2225
public List<WorkspaceResponse> getUserWorkspaces(String email) {
23-
User user = UserUtil.getUserByEmail(email);
26+
User user = util.getUserByEmail(email);
2427
List<Workspace> workspaces = workspaceRepository.findByMemberId(user.getId());
2528

2629
return workspaces.stream()
@@ -32,7 +35,7 @@ public WorkspaceResponse getWorkspace(Long workspaceId, String email) {
3235
Workspace workspace = workspaceRepository.findById(workspaceId)
3336
.orElseThrow(() -> new RuntimeException("Workspace not found or access denied"));
3437

35-
User user = UserUtil.getUserByEmail(email);
38+
User user = util.getUserByEmail(email);
3639
if (!workspace.getMembers().contains(user)) {
3740
throw new RuntimeException("Workspace not found or access denied");
3841
}
@@ -42,7 +45,7 @@ public WorkspaceResponse getWorkspace(Long workspaceId, String email) {
4245

4346
@Transactional
4447
public WorkspaceResponse createWorkspace(WorkspaceRequest request, String email) {
45-
User user = UserUtil.getUserByEmail(email);
48+
User user = util.getUserByEmail(email);
4649

4750
Workspace workspace = new Workspace();
4851
workspace.setName(request.getName());
@@ -58,7 +61,7 @@ public WorkspaceResponse updateWorkspace(Long workspaceId, WorkspaceRequest requ
5861
Workspace workspace = workspaceRepository.findById(workspaceId)
5962
.orElseThrow(() -> new RuntimeException("Workspace not found or access denied"));
6063

61-
User user = UserUtil.getUserByEmail(email);
64+
User user = util.getUserByEmail(email);
6265
if (!workspace.getMembers().contains(user)) {
6366
throw new RuntimeException("Workspace not found or access denied");
6467
}
@@ -74,7 +77,7 @@ public void deleteWorkspace(Long workspaceId, String email) {
7477
Workspace workspace = workspaceRepository.findById(workspaceId)
7578
.orElseThrow(() -> new RuntimeException("Workspace not found or access denied"));
7679

77-
User user = UserUtil.getUserByEmail(email);
80+
User user = util.getUserByEmail(email);
7881
if (!workspace.getMembers().contains(user)) {
7982
throw new RuntimeException("Workspace not found or access denied");
8083
}
@@ -87,12 +90,12 @@ public WorkspaceResponse addMember(Long workspaceId, AddMemberRequest request, S
8790
Workspace workspace = workspaceRepository.findById(workspaceId)
8891
.orElseThrow(() -> new RuntimeException("Workspace not found or access denied"));
8992

90-
User currentUser = UserUtil.getUserByEmail(email);
93+
User currentUser = util.getUserByEmail(email);
9194
if (!workspace.getMembers().contains(currentUser)) {
9295
throw new RuntimeException("Workspace not found or access denied");
9396
}
9497

95-
User userToAdd = UserUtil.getUserByEmail(request.getEmail());
98+
User userToAdd = util.getUserByEmail(request.getEmail());
9699
if (workspace.getMembers().contains(userToAdd)) {
97100
throw new RuntimeException("User is already a member of the workspace");
98101
}
@@ -109,12 +112,12 @@ public WorkspaceResponse removeMember(Long workspaceId, Long userId, String emai
109112
Workspace workspace = workspaceRepository.findById(workspaceId)
110113
.orElseThrow(() -> new RuntimeException("Workspace not found or access denied"));
111114

112-
User currentUser = UserUtil.getUserByEmail(email);
115+
User currentUser = util.getUserByEmail(email);
113116
if (!workspace.getMembers().contains(currentUser)) {
114117
throw new RuntimeException("Workspace not found or access denied");
115118
}
116119

117-
User userToRemove = UserUtil.getUserById(userId);
120+
User userToRemove = util.getUserById(userId);
118121
if (!workspace.getMembers().contains(userToRemove)) {
119122
throw new RuntimeException("User is not a member of the workspace");
120123
}

‎src/main/java/com/syncapi/util/AuthUtil.java‎

Lines changed: 0 additions & 15 deletions
This file was deleted.

‎src/main/java/com/syncapi/util/UserUtil.java‎

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.syncapi.util;
2+
3+
import com.syncapi.entity.User;
4+
import com.syncapi.repository.UserRepository;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.security.core.Authentication;
7+
import org.springframework.security.core.context.SecurityContextHolder;
8+
import org.springframework.stereotype.Component;
9+
10+
@Component
11+
public class Util {
12+
private final UserRepository userRepository;
13+
14+
@Autowired
15+
public Util(UserRepository userRepository) {
16+
this.userRepository = userRepository;
17+
}
18+
19+
public static String getCurrentUserEmail() {
20+
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
21+
if (authentication == null || !authentication.isAuthenticated()) {
22+
throw new RuntimeException("No authenticated user found");
23+
}
24+
25+
return authentication.getName();
26+
}
27+
28+
public User getUserByEmail(String email) {
29+
return userRepository.findByEmail(email)
30+
.orElseThrow(() -> new RuntimeException("User not found: " + email));
31+
}
32+
33+
public User getUserById(Long id) {
34+
return userRepository.findById(id)
35+
.orElseThrow(() -> new RuntimeException("User not found with Id: " + id));
36+
}
37+
}

0 commit comments

Comments
 (0)