Added userDAO

This commit is contained in:
Marc Beyer 2022-01-25 19:17:41 +01:00
parent 56919ab412
commit 8f04ac7ae8
7 changed files with 95 additions and 28 deletions

View File

@ -15,8 +15,6 @@ import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.List; import java.util.List;
@Controller @Controller
@ -96,7 +94,7 @@ public class EventController {
List<Event> eventList = eventDAO.getAllEventsInTimespan(authUser.getId(), startDate, endDate); List<Event> eventList = eventDAO.getAllEventsInTimespan(authUser.getId(), startDate, endDate);
return new ResponseEntity<>(JSONMapper.ToJSON(eventList), HttpStatus.OK); return new ResponseEntity<>(JSONMapper.eventListToJSON(eventList), HttpStatus.OK);
} }

View File

@ -1,6 +1,9 @@
package com.vpr.server.controller; package com.vpr.server.controller;
import com.vpr.server.dao.interfaces.UserDAO;
import com.vpr.server.data.Event;
import com.vpr.server.data.User; import com.vpr.server.data.User;
import com.vpr.server.json.JSONMapper;
import com.vpr.server.repository.UserRepository; import com.vpr.server.repository.UserRepository;
import com.vpr.server.security.Hasher; import com.vpr.server.security.Hasher;
import com.vpr.server.security.Token; import com.vpr.server.security.Token;
@ -9,19 +12,21 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException; import java.security.spec.InvalidKeySpecException;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
@Controller @Controller
@RequestMapping(path = "/user") @RequestMapping(path = "/user")
public class UserController { public class UserController {
@Autowired @Autowired
private UserRepository userRepository; private UserRepository userRepository;
@Autowired
private UserDAO userDAO;
private AuthController authController; private final AuthController authController;
public UserController() { public UserController() {
this.authController = new AuthController(); this.authController = new AuthController();
@ -146,7 +151,9 @@ public class UserController {
@GetMapping(path = "/all") @GetMapping(path = "/all")
public @ResponseBody public @ResponseBody
Object[] getAllUsers() { ResponseEntity<String> getAllUser() {
return userRepository.findAllUsernames(); List<User> userList = userDAO.getAllUser();
return new ResponseEntity<>(JSONMapper.userListToJSON(userList), HttpStatus.OK);
} }
} }

View File

@ -0,0 +1,24 @@
package com.vpr.server.dao.implementation;
import com.vpr.server.dao.interfaces.UserDAO;
import com.vpr.server.data.Event;
import com.vpr.server.data.User;
import org.springframework.stereotype.Repository;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;
import java.util.List;
@Repository
@Transactional
public class UserDAOImplementation implements UserDAO {
@PersistenceContext
private EntityManager manager;
@Override
public List<User> getAllUser() {
return manager.createNamedQuery("getAllUser", User.class).getResultList();
}
}

View File

@ -0,0 +1,9 @@
package com.vpr.server.dao.interfaces;
import com.vpr.server.data.User;
import java.util.List;
public interface UserDAO {
List<User> getAllUser();
}

View File

@ -4,7 +4,15 @@ import javax.persistence.*;
import java.util.List; import java.util.List;
// @Entity creates a table out of this class with Hibernate // @Entity creates a table out of this class with Hibernate
@Entity @Entity(name = "User")
@Table(name = "user")
@NamedNativeQueries({
@NamedNativeQuery(
name = "getAllUser",
query = "SELECT * FROM user",
resultClass = User.class
)
})
public class User { public class User {
// Generate the primary key // Generate the primary key
@Id @Id

View File

@ -1,14 +1,38 @@
package com.vpr.server.json; package com.vpr.server.json;
import com.vpr.server.data.Event; import com.vpr.server.data.Event;
import com.vpr.server.data.User;
import com.vpr.server.data.UserEvent; import com.vpr.server.data.UserEvent;
import java.sql.Time; import java.sql.Time;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class JSONMapper { public class JSONMapper {
public static List<String> ToJSON(Event event){
public static String userToJSON(User user) {
return "{" +
"\"userId\": " + user.getId() + ", " +
"\"forename\": \"" + user.getForename() + "\", " +
"\"name\": \"" + user.getName() + "\", " +
"\"login\": \"" + user.getLogin() + "\"," +
"\"isAdmin\": " + user.isAdmin() +
"}";
}
public static String userListToJSON(List<User> userList) {
StringBuilder userListJSON = new StringBuilder();
for (User user : userList) {
userListJSON.append(", ");
userListJSON.append(userToJSON(user));
}
userListJSON.delete(0, 2);
return "[" + userListJSON + "]";
}
public static List<String> eventToJSON(Event event) {
List<String> eventListJSON = new ArrayList<>(); List<String> eventListJSON = new ArrayList<>();
for (UserEvent userEvent : event.getUserEvent()) { for (UserEvent userEvent : event.getUserEvent()) {
@ -22,8 +46,8 @@ public class JSONMapper {
"\"priority\": " + event.getPriority() + "," + "\"priority\": " + event.getPriority() + "," +
"\"fullDay\": " + event.isFullDay() + "," + "\"fullDay\": " + event.isFullDay() + "," +
"\"private\": " + event.isPrivate() + "," + "\"private\": " + event.isPrivate() + "," +
"\"start\": " + ToJSON(event.getStart()) + "," + "\"start\": " + timeToJSON(event.getStart()) + "," +
"\"end\": " + ToJSON(event.getEnd()) + "\"end\": " + timeToJSON(event.getEnd()) +
"}"; "}";
eventListJSON.add(eventJSON); eventListJSON.add(eventJSON);
@ -32,11 +56,11 @@ public class JSONMapper {
return eventListJSON; return eventListJSON;
} }
public static String ToJSON(List<Event> eventList){ public static String eventListToJSON(List<Event> eventList) {
StringBuilder eventListJSON = new StringBuilder(); StringBuilder eventListJSON = new StringBuilder();
for(Event event : eventList){ for (Event event : eventList) {
List<String> eventsJSON = ToJSON(event); List<String> eventsJSON = eventToJSON(event);
for(String eventJSON : eventsJSON){ for (String eventJSON : eventsJSON) {
eventListJSON.append(", "); eventListJSON.append(", ");
eventListJSON.append(eventJSON); eventListJSON.append(eventJSON);
} }
@ -46,8 +70,8 @@ public class JSONMapper {
return "[" + eventListJSON + "]"; return "[" + eventListJSON + "]";
} }
public static String ToJSON(Time time){ public static String timeToJSON(Time time) {
if(time == null){ if (time == null) {
return "null"; return "null";
} }

View File

@ -78,7 +78,4 @@ public interface EventRepository extends CrudRepository<Event, Integer> {
nativeQuery = true nativeQuery = true
) )
void deleteById(long id); void deleteById(long id);
//@Query(nativeQuery = true)
//List<Event> findEventsInDateRange(Long userId, String startDate, String endDate);
} }