From 540d3a130f9458cc7a127c5b6a729c1f972c225f Mon Sep 17 00:00:00 2001 From: Marc Beyer Date: Sun, 28 Nov 2021 17:37:10 +0100 Subject: [PATCH] Fixed date conversion --- .../java/com/vpr/server/MainController.java | 66 +++++++++++++++++++ .../java/com/vpr/server/UserRepository.java | 1 + 2 files changed, 67 insertions(+) diff --git a/server/src/main/java/com/vpr/server/MainController.java b/server/src/main/java/com/vpr/server/MainController.java index 2afbf77..d27751b 100644 --- a/server/src/main/java/com/vpr/server/MainController.java +++ b/server/src/main/java/com/vpr/server/MainController.java @@ -5,6 +5,9 @@ import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import java.sql.Date; +import java.sql.Time; +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) @@ -44,6 +47,69 @@ public class MainController { return "Saved"; } + @PostMapping(path="/add-event") + public @ResponseBody String addEvent ( + @RequestParam Integer userId, + @RequestParam String date, + @RequestParam String name, + @RequestParam String start, + @RequestParam String end, + @RequestParam Integer prority, + @RequestParam Boolean isFullDay, + @RequestParam Boolean isPrivate + ) { + + com.vpr.server.Event event = new com.vpr.server.Event(); + + event.setName(name); + + try { + SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm"); + long ms = simpleDateFormat.parse(start).getTime(); + event.setStart(new Time(ms)); + }catch (Exception e){ + event.setStart(null); + } + + try { + SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm"); + long ms = simpleDateFormat.parse(end).getTime(); + event.setEnd(new Time(ms)); + }catch (Exception e){ + event.setEnd(null); + } + + event.setPriority(prority); + 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){ + System.out.println("DATE FORMAT NOT CORRECT"); + } + + userEvent.setEvent(event); + + long uId = Long.valueOf(userId); + User user = userRepository.findById(uId); + userEvent.setUser(user); + + System.out.println(userEvent); + System.out.println(user); + + userEventRepository.save(userEvent); + return "Saved"; + } + // GET-request at /all-users // returns JSON-data @GetMapping(path="/all-users") diff --git a/server/src/main/java/com/vpr/server/UserRepository.java b/server/src/main/java/com/vpr/server/UserRepository.java index 010bea3..6b2874c 100644 --- a/server/src/main/java/com/vpr/server/UserRepository.java +++ b/server/src/main/java/com/vpr/server/UserRepository.java @@ -15,4 +15,5 @@ public interface UserRepository extends CrudRepository { nativeQuery = true) Object[] findAllUsernames(); + com.vpr.server.User findById(long id); } \ No newline at end of file