From 8d6a850d3030847371baef6dfd635b1d89cf6210 Mon Sep 17 00:00:00 2001 From: Marc Beyer Date: Mon, 20 Dec 2021 19:44:01 +0100 Subject: [PATCH] Added login endpoint --- .../java/com/vpr/server/MainController.java | 74 +++++++++++++------ server/src/main/java/com/vpr/server/User.java | 11 +++ .../java/com/vpr/server/UserRepository.java | 2 + 3 files changed, 63 insertions(+), 24 deletions(-) diff --git a/server/src/main/java/com/vpr/server/MainController.java b/server/src/main/java/com/vpr/server/MainController.java index 2f137b9..a56d476 100644 --- a/server/src/main/java/com/vpr/server/MainController.java +++ b/server/src/main/java/com/vpr/server/MainController.java @@ -1,8 +1,11 @@ package com.vpr.server; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; +import org.springframework.web.server.ResponseStatusException; import java.sql.Date; import java.sql.Time; @@ -10,7 +13,7 @@ import java.text.SimpleDateFormat; import java.util.Optional; @Controller // This means that this class is a Controller -@RequestMapping(path="/vpr") // This means URL's start with /demo (after Application path) +@RequestMapping(path = "/vpr") // This means URL's start with /demo (after Application path) public class MainController { // This means to get the bean called userRepository @@ -26,13 +29,14 @@ public class MainController { // POST-request at /add with request parameter // @ResponseBody means the returned String is the response, not a view name - @PostMapping(path="/add-user") - public @ResponseBody String addNewUser ( + @PostMapping(path = "/add-user") + public @ResponseBody + String addNewUser( @RequestParam String name, @RequestParam String forename, @RequestParam String password, @RequestParam String isAdmin - ) { + ) { com.vpr.server.User user = new com.vpr.server.User(); @@ -47,8 +51,22 @@ public class MainController { return "Saved"; } - @PostMapping(path="/add-event") - public @ResponseBody String addEvent ( + @PostMapping(path = "/login") + public @ResponseBody + String login( + @RequestParam String login, + @RequestParam String password + ) { + User user = userRepository.findByLoginAndPassword(login, password); + if(user != null){ + return "" + user.getId(); + } + return "-1"; + } + + @PostMapping(path = "/add-event") + public @ResponseBody + ResponseEntity addEvent( @RequestParam Integer userId, @RequestParam String date, @RequestParam String name, @@ -58,16 +76,23 @@ public class MainController { @RequestParam Boolean isFullDay, @RequestParam Boolean isPrivate ) { + String errorString = ""; com.vpr.server.Event event = new com.vpr.server.Event(); - event.setName(name); + System.out.println(name.length() + ". name " + name); + if (name.length() > 3) { + event.setName(name); + } else { + System.out.println("NAME IST ZU KURZ"); + return new ResponseEntity(HttpStatus.NOT_ACCEPTABLE); + } try { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm"); long ms = simpleDateFormat.parse(start).getTime(); event.setStart(new Time(ms)); - }catch (Exception e){ + } catch (Exception e) { event.setStart(null); } @@ -75,7 +100,7 @@ public class MainController { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm"); long ms = simpleDateFormat.parse(end).getTime(); event.setEnd(new Time(ms)); - }catch (Exception e){ + } catch (Exception e) { event.setEnd(null); } @@ -83,22 +108,17 @@ public class MainController { event.setFullDay(isFullDay); event.setPrivate(isPrivate); - - eventRepository.save(event); - - com.vpr.server.UserEvent userEvent = new com.vpr.server.UserEvent(); try { System.out.println("date " + date); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); userEvent.setDate(new java.sql.Date(simpleDateFormat.parse(date).getTime())); - }catch (Exception e){ + } catch (Exception e) { System.out.println("DATE FORMAT NOT CORRECT"); } userEvent.setEvent(event); - long uId = Long.valueOf(userId); User user = userRepository.findById(uId); userEvent.setUser(user); @@ -106,12 +126,15 @@ public class MainController { System.out.println(userEvent); System.out.println(user); + eventRepository.save(event); userEventRepository.save(userEvent); - return "Saved"; + + return new ResponseEntity(HttpStatus.OK); } - @PostMapping(path="/del-event") - public @ResponseBody String addEvent ( @RequestParam Integer eventId ) { + @PostMapping(path = "/del-event") + public @ResponseBody + String addEvent(@RequestParam Integer eventId) { eventRepository.deleteUserEventsById(Long.valueOf(eventId)); eventRepository.deleteById(Long.valueOf(eventId)); return "Deleted"; @@ -119,21 +142,24 @@ public class MainController { // GET-request at /all-users // returns JSON-data - @GetMapping(path="/all-users") - public @ResponseBody Object[] getAllUsers() { + @GetMapping(path = "/all-users") + public @ResponseBody + Object[] getAllUsers() { return userRepository.findAllUsernames(); } // POST-request at /all-events // returns JSON-data - @PostMapping(path="/all-events") - public @ResponseBody Object[] getAllEvents(@RequestParam long userId) { + @PostMapping(path = "/all-events") + public @ResponseBody + Object[] getAllEvents(@RequestParam long userId) { return eventRepository.findAllVisibleByUserId(userId); } - @GetMapping(path="/all-events-test") - public @ResponseBody Iterable getAllEventsTest() { + @GetMapping(path = "/all-events-test") + public @ResponseBody + Iterable getAllEventsTest() { return eventRepository.findAll(); } diff --git a/server/src/main/java/com/vpr/server/User.java b/server/src/main/java/com/vpr/server/User.java index 0f00fe6..915eb23 100644 --- a/server/src/main/java/com/vpr/server/User.java +++ b/server/src/main/java/com/vpr/server/User.java @@ -17,6 +17,9 @@ public class User { @Column(name="forename", nullable=false) private String forename; + @Column(name="login", nullable=false) + private String login; + @Column(name="password", nullable=false) private String password; @@ -57,6 +60,14 @@ public class User { this.forename = forename; } + public String getLogin() { + return login; + } + + public void setLogin(String login) { + this.login = login; + } + public String getPassword() { return password; } diff --git a/server/src/main/java/com/vpr/server/UserRepository.java b/server/src/main/java/com/vpr/server/UserRepository.java index 6b2874c..eb2ba4b 100644 --- a/server/src/main/java/com/vpr/server/UserRepository.java +++ b/server/src/main/java/com/vpr/server/UserRepository.java @@ -16,4 +16,6 @@ public interface UserRepository extends CrudRepository { Object[] findAllUsernames(); com.vpr.server.User findById(long id); + + com.vpr.server.User findByLoginAndPassword(String login, String password); } \ No newline at end of file