Skip to content

Commit

Permalink
refactor: Utils is now autowired and avoids repetitive code inside co…
Browse files Browse the repository at this point in the history
…ntrollers + added method getCurrentUserRole
  • Loading branch information
franchescoURJC committed Mar 18, 2023
1 parent 2a4a484 commit 6d8026f
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class AdminPanelController {
private TopicService topicService;
@Autowired
private PostService postService;
@Autowired
private Utils utils;

@GetMapping("/admin-panel")
public String adminPanel(Model model) {
Expand All @@ -41,7 +43,6 @@ public String adminPanel(Model model) {
List<Topic> topicList = topicService.findAll();
model.addAttribute("topics", topicList);

Utils utils = new Utils(userService, postService);
utils.searchBarInitializer(model);
return "admin-panel";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,15 @@ public class CreateListController {

@Autowired
TopicService topicService;

@Autowired
PostItemService postItemService;

@Autowired
private UserService userService;

@Autowired
PostService postService;
@Autowired
private Utils utils;

@GetMapping("/create-list")
public String createList(Model model) {
Utils utils = new Utils(userService, postService);
utils.searchBarInitializer(model);
List<String> topicOptions = topicService.getAllTopics();
model.addAttribute("topicOptions", topicOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,13 @@
@Controller
public class PostController {

@Autowired
private UserService userService;

@Autowired
private PostService postService;

@Autowired
private VotesService votesService;
private Utils utils;

@GetMapping({"/","/followed-users/"})
public String home(Model model) {
Utils utils = new Utils(userService, postService);
utils.searchBarInitializer(model);
return "feed";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ public class TopListController {
private User userProfile;
private Post post;

@Autowired
private Utils utils;

@GetMapping("/top-list/{id}")
public String topList(Model model, Authentication authentication, @PathVariable long id){
Utils utils = new Utils(userService, postService);
utils.searchBarInitializer(model);

if(authentication == null){
Expand Down
22 changes: 18 additions & 4 deletions back/src/main/java/net/daw/alist/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import javax.sql.rowset.serial.SerialBlob;
import java.io.IOException;
import java.nio.file.attribute.UserPrincipalNotFoundException;
import java.sql.Blob;
import java.sql.SQLException;
import java.util.List;
import java.util.Optional;

@Service
public class Utils {
public Utils(UserService userService, PostService postService) {
this.userService = userService;
this.postService = postService;
}

@Autowired
private UserService userService;
Expand All @@ -40,4 +41,17 @@ public void searchBarInitializer(Model model){
model.addAttribute("searchSuggestedPosts", postList);
}

public String getCurrentUserRole(Authentication authentication) {
if(authentication == null)
return "GUEST";
User loggedUser = (User) authentication.getPrincipal();
Optional<User> user = userService.findByID(loggedUser.getId());
if (user.isPresent()){
return user.get().getRole().toString();
}
else{
throw new RuntimeException();
}
}

}

0 comments on commit 6d8026f

Please sign in to comment.