Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

import wp_project.event_manager.model.Location;

import java.util.List;

public interface LocationService {

List<Location> listAll();

Location findById(Long id);

Location create(Long latitude, Long longitude, String name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
import wp_project.event_manager.model.Role;
import wp_project.event_manager.model.User;

import java.util.List;

public interface UserService {

List<User> listAll();

User findByUsername(String username);

User create(String username, String password, Role role);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import wp_project.event_manager.repository.LocationRepository;
import wp_project.event_manager.service.LocationService;

import java.util.List;

@Service
public class LocationServiceImpl implements LocationService {
private final LocationRepository locationRepository;
Expand All @@ -14,6 +16,11 @@ public LocationServiceImpl(LocationRepository locationRepository) {
this.locationRepository = locationRepository;
}

@Override
public List<Location> listAll() {
return this.locationRepository.findAll();
}

@Override
public Location findById(Long id) {
return this.locationRepository.findById(id).orElseThrow(InvalidLocationIdException::new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import wp_project.event_manager.repository.UserRepository;
import wp_project.event_manager.service.UserService;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
private final UserRepository userRepository;
Expand All @@ -15,6 +17,11 @@ public UserServiceImpl(UserRepository userRepository) {
this.userRepository = userRepository;
}

@Override
public List<User> listAll() {
return this.userRepository.findAll();
}

@Override
public User findByUsername(String username) {
return this.userRepository.findById(username).orElseThrow(InvalidUserIdException::new);
Expand Down
95 changes: 95 additions & 0 deletions src/main/java/wp_project/event_manager/web/EventsController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package wp_project.event_manager.web;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import wp_project.event_manager.model.Event;
import wp_project.event_manager.model.Location;
import wp_project.event_manager.model.User;
import wp_project.event_manager.service.EventService;
import wp_project.event_manager.service.LocationService;
import wp_project.event_manager.service.UserService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class EventsController {

private final EventService eventService;
private final LocationService locationService;
private final UserService userService;

public EventsController(EventService eventService, LocationService locationService, UserService userService) {
this.eventService = eventService;
this.locationService = locationService;
this.userService = userService;
}

@GetMapping({"/", "/events"})
public String showEvents(@RequestParam(required = false) String nameSearch,
@RequestParam(required = false) Long locationId,
@RequestParam(required = false) Date time,
Model model) {
List<Event> events;
List<Location> locations;

events = this.eventService.listEventsByNameAndLocationAndTime(nameSearch, locationId, time);
locations = this.locationService.listAll();
model.addAttribute("events", events);
model.addAttribute("locations", locations);
return "list.html";
}

@GetMapping("/events/create")
public String showCreate(Model model) {
List<Location> locations = this.locationService.listAll();
List<User> users = this.userService.listAll();
model.addAttribute("locations", locations);
model.addAttribute("users", users);
return "form.html";
}

@GetMapping("/events/{id}/edit")
public String showEdit(@PathVariable Long id, Model model) {
Event event = this.eventService.findById(id);

List<Location> locations = this.locationService.listAll();
List<User> users = this.userService.listAll();

model.addAttribute("event", event);
model.addAttribute("locations", locations);
model.addAttribute("users", users);
return "form.html";
}

@PostMapping("/events")
public String create(@RequestParam String name,
@RequestParam List<String> attendeeUsernames,
@RequestParam Long locationId,
@RequestParam Date time) {
this.eventService.create(name, attendeeUsernames, locationId, time, new ArrayList<>());
return "redirect:/events";
}

@PostMapping("/events/{id}")
public String update(@PathVariable Long id,
@RequestParam String name,
@RequestParam List<String> attendeeUsernames,
@RequestParam Long locationId,
@RequestParam Date time,
@RequestParam List<Long> commentIds) {
this.eventService.update(id, name, attendeeUsernames, locationId, time, commentIds);
return "redirect:/events";
}

@PostMapping("/events/{id}/delete")
public String delete(@PathVariable Long id) {
this.eventService.delete(id);
return "redirect:/events";
}
}