Added login endpoint

This commit is contained in:
Marc Beyer 2021-12-20 19:44:01 +01:00
parent bfbcd5c44b
commit 8d6a850d30
3 changed files with 63 additions and 24 deletions

View File

@ -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;
@ -27,7 +30,8 @@ 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 (
public @ResponseBody
String addNewUser(
@RequestParam String name,
@RequestParam String forename,
@RequestParam String password,
@ -47,8 +51,22 @@ public class MainController {
return "Saved";
}
@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 String addEvent (
public @ResponseBody
ResponseEntity addEvent(
@RequestParam Integer userId,
@RequestParam String date,
@RequestParam String name,
@ -58,10 +76,17 @@ public class MainController {
@RequestParam Boolean isFullDay,
@RequestParam Boolean isPrivate
) {
String errorString = "";
com.vpr.server.Event event = new com.vpr.server.Event();
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");
@ -83,10 +108,6 @@ public class MainController {
event.setFullDay(isFullDay);
event.setPrivate(isPrivate);
eventRepository.save(event);
com.vpr.server.UserEvent userEvent = new com.vpr.server.UserEvent();
try {
@ -98,7 +119,6 @@ public class MainController {
}
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 ) {
public @ResponseBody
String addEvent(@RequestParam Integer eventId) {
eventRepository.deleteUserEventsById(Long.valueOf(eventId));
eventRepository.deleteById(Long.valueOf(eventId));
return "Deleted";
@ -120,20 +143,23 @@ public class MainController {
// GET-request at /all-users
// returns JSON-data
@GetMapping(path = "/all-users")
public @ResponseBody Object[] getAllUsers() {
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) {
public @ResponseBody
Object[] getAllEvents(@RequestParam long userId) {
return eventRepository.findAllVisibleByUserId(userId);
}
@GetMapping(path = "/all-events-test")
public @ResponseBody Iterable<com.vpr.server.Event> getAllEventsTest() {
public @ResponseBody
Iterable<com.vpr.server.Event> getAllEventsTest() {
return eventRepository.findAll();
}

View File

@ -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;
}

View File

@ -16,4 +16,6 @@ public interface UserRepository extends CrudRepository<User, Integer> {
Object[] findAllUsernames();
com.vpr.server.User findById(long id);
com.vpr.server.User findByLoginAndPassword(String login, String password);
}