Skip to content

Commit

Permalink
For retrieving all the admin messages, EntityManager and JdbcTemplate…
Browse files Browse the repository at this point in the history
… used for querying instead of using repository.
  • Loading branch information
Badri Paudel committed Mar 16, 2024
1 parent bf5caa6 commit ca1b946
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 10 deletions.
10 changes: 8 additions & 2 deletions src/main/java/info/keeper/controllers/AdminController.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,16 @@ public String showAdminPage(Principal principal, Model model) {
@PostMapping("/saveMessage")
public String saveMessage(@ModelAttribute("adminMessage")AdminMessage adminMessage,
Principal principal, HttpSession session) {
saveAdminMessage(adminMessage, principal);
session.setAttribute("message", new Message("You have sent notice to everyone", "alert-success"));
return "redirect:/admin/index/";
}

// TODO: Move to a separate admin service file.
private void saveAdminMessage(AdminMessage adminMessage, Principal principal) {
adminMessage.setDate(new Date());
adminMessage.setPostedBy(principal.getName().split("@")[0]); // show only before @
adminRepository.save(adminMessage);
session.setAttribute("message", new Message("You have sent notice to everyone", "alert-success"));
return "redirect:/admin/index/";

}
}
14 changes: 7 additions & 7 deletions src/main/java/info/keeper/controllers/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import info.keeper.repositories.AdminRepository;
import info.keeper.repositories.ContactRepository;
import info.keeper.repositories.UserRepository;
import info.keeper.service.UserService;
import info.keeper.utils.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
Expand Down Expand Up @@ -37,28 +38,27 @@ public class UserController {

private UserRepository userRepository;
private AdminRepository adminRepository;
private UserService userService;

public UserController(UserRepository userRepository, AdminRepository adminRepository) {
public UserController(UserRepository userRepository, AdminRepository adminRepository, UserService userService) {
this.userRepository = userRepository;
this.adminRepository = adminRepository;
this.userService = userService;
}

@GetMapping
public String dashboard(Model model, Principal principal) {
String username = principal.getName();
//get user using username
User user = userRepository.findUserByUsername(username);

User user = userService.getUserByUsername(principal.getName());
model.addAttribute("title", "User Dashboard Page - Information Keeper");

model.addAttribute("user", user);
return "normal_user/user_dashboard";
}

//show profile page
@GetMapping("/profile/")
public String showProfileOfUser(Model model, Principal principal) {
User user = userRepository.findUserByUsername(principal.getName());
User user = userService.getUserByUsername(principal.getName());

model.addAttribute("user", user);
model.addAttribute("title", "Profile Page - " + user.getName());
Expand All @@ -69,7 +69,7 @@ public String showProfileOfUser(Model model, Principal principal) {
@GetMapping("/admin-notice")
public String aboutPage(Model model) {
model.addAttribute("title", "Admin Notice - Information Keeper");
ArrayList<AdminMessage> adminMessages = adminRepository.findAll();
ArrayList<AdminMessage> adminMessages = userService.getAllAdminMessages();
model.addAttribute("adminMsgList", adminMessages);
return "normal_user/admin_notice"; // return home.html page from templates folder
}
Expand Down
30 changes: 29 additions & 1 deletion src/main/java/info/keeper/service/UserService.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,48 @@
package info.keeper.service;

import info.keeper.models.AdminMessage;
import info.keeper.models.User;
import info.keeper.repositories.AdminRepository;
import info.keeper.repositories.UserRepository;
import info.keeper.utils.UserRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserService {
private UserRepository userRepository;
private JdbcTemplate jdbcTemplate;
private EntityManager entityManager;

public UserService(UserRepository userRepository) {
public UserService(UserRepository userRepository,
JdbcTemplate jdbcTemplate, EntityManager entityManager) {
this.userRepository = userRepository;
this.jdbcTemplate = jdbcTemplate;
this.entityManager = entityManager;
}

public User getUserByUsername(String username) {
User user = userRepository.findUserByUsername(username);
return user;
}

public ArrayList<AdminMessage> getAllAdminMessages() {
String query = "SELECT * FROM admin_message";
Query namedQuery = this.entityManager.createNativeQuery(query, AdminMessage.class);
return (ArrayList<AdminMessage>)namedQuery.getResultList();
// return getAllAdminMessagesUsingJdbcT(); // Same as above.
}

// Using JdbcTemplate
public ArrayList<AdminMessage> getAllAdminMessagesUsingJdbcT() {
String query = "SELECT * FROM admin_message";
List<AdminMessage> adminMessageList = this.jdbcTemplate.query(query, new UserRowMapper());
return (ArrayList<AdminMessage>) adminMessageList;
}
}

23 changes: 23 additions & 0 deletions src/main/java/info/keeper/utils/UserRowMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package info.keeper.utils;

import info.keeper.models.AdminMessage;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
* To map data selected from the database to the object
*/
public class UserRowMapper implements RowMapper<AdminMessage> {

@Override
public AdminMessage mapRow(ResultSet resultSet, int i) throws SQLException {
AdminMessage user = new AdminMessage();
user.setId(resultSet.getInt("admin_message_id"));
user.setDate(resultSet.getTimestamp("posted_data"));
user.setMessage(resultSet.getString("admin_message"));
user.setPostedBy(resultSet.getString("posted_by"));
return user;
}
}

0 comments on commit ca1b946

Please sign in to comment.