Skip to content
Snippets Groups Projects
Commit 1d8aecc4 authored by Harrison Davies's avatar Harrison Davies
Browse files

Merge branch '223-update-the-existing-event' into 'release/Sprint-3'

Resolve "As an admin user, I must be able to edit or delete an existing event so that I can update the change in event information or delete them if it's no longer relevant."

See merge request c24108486/team-11-polish-community-group!45
parents c153398f c3cc1bbc
No related branches found
No related tags found
No related merge requests found
Showing
with 486 additions and 27 deletions
package polish_community_group_11.polish_community.event.controllers;
import org.springframework.web.bind.annotation.PathVariable;
import jakarta.validation.Valid;
import org.springframework.security.core.parameters.P;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import polish_community_group_11.polish_community.event.services.EventRepository;
import polish_community_group_11.polish_community.event.models.Event;
import polish_community_group_11.polish_community.event.models.EventImpl;
import polish_community_group_11.polish_community.event.services.EventService;
import java.sql.SQLException;
@Controller
public class EventController {
// Inject the EventRepository service to handle event-related data operations
private EventRepository eventService;
private EventService eventService;
// Constructor-based dependency injection for EventRepository
public EventController(EventRepository eventService){
public EventController(EventService eventService){
this.eventService = eventService;
}
......@@ -50,4 +57,29 @@ public class EventController {
// Return the populated ModelAndView object, which will render the event details view
return modelAndView;
}
//Update Events
@GetMapping("event/editEvent/{id}")
public ModelAndView editEvent(@PathVariable int id ){
Event event = eventService.getEventById(id);
event.setEvent_date(event.getEvent_date());
ModelAndView modelAndView = new ModelAndView("event/editEvent");
modelAndView.addObject("editEvent",event);
return modelAndView;
}
@PostMapping("/event/editEvent")
public ModelAndView editEvent(@ModelAttribute("editEvent") EventImpl editEvent, BindingResult bindingResult, Model model) throws SQLException {
ModelAndView modelAndView = new ModelAndView("event/editEvent");
if(bindingResult.hasErrors()){
modelAndView.addObject(model.asMap());
}
else{
int id = editEvent.getEvent_id();
eventService.getEditEvent(editEvent);
modelAndView.setViewName("redirect:/event");
}
return modelAndView;
}
}
package polish_community_group_11.polish_community.event.services;
package polish_community_group_11.polish_community.event.dao;
import polish_community_group_11.polish_community.event.models.Event;
import java.sql.SQLException;
import java.util.List;
public interface EventRepository {
......@@ -8,4 +10,5 @@ public interface EventRepository {
List<Event> getAllEvents();
Event getEventById(int id);
Event editEvent(Event event)throws SQLException;
}
package polish_community_group_11.polish_community.event.services;
package polish_community_group_11.polish_community.event.dao;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import polish_community_group_11.polish_community.event.models.Event;
import polish_community_group_11.polish_community.event.models.EventImpl;
import java.sql.SQLException;
import java.util.List;
@Repository // Marks this class as a Spring Data repository for database interaction
......@@ -74,4 +77,34 @@ public class EventRepositoryImpl implements EventRepository {
return event;
}
// Updates selected record with new updated information
public Event editEvent(Event event)throws SQLException {
String updateSql = "UPDATE event SET event_title = ?, description = ?, event_date = ?, event_time = ?, " +
"location = ?, user_id = ?, imageUrl = ?, whyJoin = ?, benefits = ? WHERE event_id = ?";
try {
// jdbc.update() is a method that will execute the sql query
// replaces the ? with the actual values from the news object
int rowsAffected = jdbc.update(updateSql,
event.getEvent_title(),
event.getDescription(),
event.getEvent_date(),
event.getEvent_time(),
event.getLocation(),
1,
event.getImageUrl(),
event.getWhyJoin(),
event.getBenefits(),
event.getEvent_id()
);
// error handling
if (rowsAffected == 0) {
throw new SQLException("No event item was updated. Check the ID provided.");
}
} catch (DataAccessException e) {
throw new SQLException("Error updating event with ID: " + event.getEvent_id(), e);
}
return event;
}
}
package polish_community_group_11.polish_community.event.services;
import polish_community_group_11.polish_community.event.models.Event;
import java.sql.SQLDataException;
import java.sql.SQLException;
import java.util.List;
public interface EventService {
List<Event> getAllEvents();
Event getEventById(int id);
Event getEditEvent(Event event) throws SQLException;
}
package polish_community_group_11.polish_community.event.services;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import polish_community_group_11.polish_community.event.dao.EventRepository;
import polish_community_group_11.polish_community.event.models.Event;
import java.sql.SQLException;
import java.util.List;
@Service
public class EventServiceImpl implements EventService {
private final EventRepository eventRepository;
@Autowired
public EventServiceImpl(EventRepository eventRepository){
this.eventRepository = eventRepository;
}
public List<Event> getAllEvents(){
return eventRepository.getAllEvents();
}
public Event getEventById(int id){
return eventRepository.getEventById(id);
}
public Event getEditEvent(Event event) throws SQLException {
return eventRepository.editEvent(event);
}
}
/* General Styles */
/* Center the form on the screen */
section {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
/*height: 100vh;*/
padding: 0 20px;
}
/* Form styling */
form {
background-color: var(--secondary-color);
border-radius: 10px;
padding: 30px;
width: 100%;
max-width: 600px;
box-sizing: border-box;
margin: auto;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); /* Add some shadow to give the form a floating effect */
}
/* Header */
h1 {
text-align: left;
margin-bottom: 20px;
font-size: 24px;
color: #333;
}
/* Label styling */
label {
display: block;
font-weight: bold;
margin-bottom: 10px;
color: #333;
font-size: 16px;
}
/* Input and Textarea Styling */
input[type="text"], input[type="date"], input[type="time"], input[type="url"], textarea {
width: 100%;
padding: 12px;
margin-bottom: 20px;
border: 1px solid var(--border-color);
border-radius: 5px;
font-size: 16px;
color: #333;
background-color: var(--background-color); /* Light background for inputs */
}
textarea {
resize: vertical;
min-height: 120px;
}
/* Alert message styling for errors */
.alert {
color: var(--secondary-color);
background-color: var(--border-color);
padding: 5px;
border-radius: 5px;
font-size: 14px;
}
.alert-warning {
color: #8a6d3b;
background-color: #fcf8e3;
}
/* Button styling */
.buttons {
display: flex;
justify-content: space-between;
gap: 15px;
}
/* Submit and Cancel button styling */
button.submit {
background-color: #5bc0de;
color: white;
border: none;
padding: 12px 20px;
font-size: 16px;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
}
button.submit:hover {
background-color: #31b0d5;
}
#cancelButton {
background-color: #f44336;
color: white;
border: none;
padding: 12px 20px;
font-size: 16px;
border-radius: 5px;
cursor: pointer;
text-decoration: none; /* Remove underline */
display: inline-block;
text-align: center;
transition: background-color 0.3s;
}
#cancelButton:hover {
background-color: #e53935;
}
/* Responsive Styles */
@media (max-width: 768px) {
section {
padding: 20px;
}
form {
width: 100%;
max-width: 90%;
padding: 20px;
}
h1 {
font-size: 20px;
}
}
/*!* General Styles *!*/
/*body {*/
/* font-family: Arial, sans-serif;*/
/* margin: 0;*/
/* padding: 0;*/
/* background-color: #E3E2DF;*/
/*}*/
/*!* Main container for event details *!*/
/*#event-details {*/
/* padding: 20px;*/
/* background-color: #E3E2DF;*/
/*}*/
/*!* Event grid container for dynamically generated cards *!*/
/*.event-grid {*/
/* display: grid;*/
/* grid-template-columns: repeat(3, 1fr);*/
/* gap: 20px; !* Equal spacing between grid items *!*/
/* row-gap: 4rem;*/
/* justify-items: center;*/
/*}*/
/*!* Styling each event card *!*/
/*.event-card {*/
/* background: #fff;*/
/* border: 1px solid #ddd;*/
/* border-radius: 10px;*/
/* overflow: hidden;*/
/* transition: transform 0.3s ease, box-shadow 0.3s ease;*/
/* width: 100%;*/
/* max-width: 300px; !* Ensures cards do not get too wide *!*/
/* box-shadow: 0 0 15px 2px #E3AFBC;*/
/*}*/
/*!* Hover effect for the card *!*/
/*.event-card:hover {*/
/* transform: translateY(-10px);*/
/* box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);*/
/*}*/
/*!* Event poster styling *!*/
/*.event-poster {*/
/* position: relative;*/
/* overflow: hidden;*/
/* height: 200px;*/
/* justify-content: center;*/
/* align-items: center;*/
/*}*/
/*.event-poster img {*/
/* width: 100%;*/
/* height: 100%;*/
/* object-fit: cover; !* Ensures the image covers the container *!*/
/*}*/
/*!* Title styling *!*/
/*.event-card h2,*/
/*.event-card h3 {*/
/* font-size: 1.5em;*/
/* color: #5D001E;*/
/* margin: 10px 0;*/
/* text-align: center;*/
/*}*/
/*!* Event details text *!*/
/*.event-card p {*/
/* font-size: 1em;*/
/* color: #666;*/
/* margin: 10px;*/
/*}*/
/*.event-card-button{*/
/* display: flex;*/
/*}*/
/*!* Register button styling *!*/
/*.register-button {*/
/* display: block;*/
/* width: 40%; !* Ensures the button spans the full width of its container *!*/
/* max-width: 250px; !* You can set a max-width to control the button size *!*/
/* padding: 10px;*/
/* margin: 10px auto; !* Horizontally centers the button and adds vertical spacing *!*/
/* background-color: var(--primary-color);*/
/* color: var(--secondary-color);*/
/* border: none;*/
/* border-radius: 5px;*/
/* cursor: pointer;*/
/* font-size: 1em;*/
/*}*/
/*.register-button:hover {*/
/* background-color: var(--border-color);*/
/*}*/
/*!* Remove underline from links with the class 'event-link' *!*/
/*.event-link {*/
/* text-decoration: none; !* Removes the underline *!*/
/* margin:auto;*/
/* background-color: var(--primary-color);*/
/* color: var(--secondary-color);*/
/*}*/
/*.event-link button{*/
/* background-color: var(--primary-color);*/
/* color: var(--secondary-color);*/
/* width: 40%; !* Ensures the button spans the full width of its container *!*/
/* max-width: 250px; !* You can set a max-width to control the button size *!*/
/* border: none;*/
/* border-radius: 5px;*/
/* cursor: pointer;*/
/* font-size: 1em;*/
/*}*/
/*!* Responsive adjustments *!*/
/*!* On medium screens, switch to a 2-column layout *!*/
/*@media (max-width: 768px) {*/
/* .event-grid {*/
/* grid-template-columns: repeat(2, 1fr);*/
/* }*/
/*}*/
/*!* On smaller screens, switch to a single column layout *!*/
/*@media (max-width: 480px) {*/
/* .event-grid {*/
/* grid-template-columns: 1fr; !* Single column for very small screens *!*/
/* }*/
/*}*/
/* General Styles */
body {
font-family: Arial, sans-serif;
......@@ -70,27 +201,45 @@ body {
margin: 10px;
}
/* Remove underline from links with the class 'event-link' */
.event-link {
text-decoration: none; /* Removes the underline */
/* Event buttons container */
.event-card-button {
display: flex;
flex-direction: column;
align-items: center; /* Center buttons horizontally */
gap: 10px; /* Adds space between buttons */
}
/* Register button styling */
.register-button {
display: block;
width: 50%; /* Ensures the button spans the full width of its container */
max-width: 300px; /* You can set a max-width to control the button size */
/* Common button styling for both buttons */
.event-card-button button {
max-width: 250px; /* Controls the button size */
padding: 10px;
margin: 10px auto; /* Horizontally centers the button and adds vertical spacing */
background-color: #EE4C7C;
color: #fff;
background-color: var(--primary-color);
color: var(--secondary-color);
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 1em;
transition: background-color 0.3s ease, transform 0.3s ease; /* Add transition effects */
}
/* Hover effect for the buttons */
.event-card-button button:hover {
background-color: var(--border-color);
transform: scale(1.05); /* Slight scaling effect on hover */
}
/* Remove underline from links with the class 'event-link' */
.event-link {
text-decoration: none; /* Removes the underline */
margin:auto;
}
.register-button:hover {
background-color: #9A1750;
.event-link button {
max-width: 250px; /* Controls the button size */
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 1em;
}
/* Responsive adjustments */
......
body{
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
.form-container {
display: flex;
justify-content: center;
......
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{layout/layout}">
<head>
<meta charset="UTF-8">
<title>Edit Event</title>
<link rel="stylesheet" href="/css/events/editEvent.css"></link>
</head>
<section layout:fragment="content">
<h1>Add New Information</h1>
<form th:action="@{/event/editEvent}" th:method="post" th:object="${editEvent}" id="addEventForm">
<!-- Hidden Event ID -->
<input th:field="*{event_id}" type="hidden" />
<!-- Event Title -->
<label th:for="*{event_title}">Event Title</label>
<input type="text" th:field="*{event_title}" placeholder="Enter the title for the event...">
<div class="alert alert-warning" th:errors="*{event_title}" th:if="${#fields.hasErrors('event_title')}">Title Error</div>
<!-- Event Description -->
<label th:for="*{description}">Event Description</label>
<textarea th:field="*{description}" placeholder="Enter the event description..."></textarea>
<div class="alert alert-warning" th:errors="*{description}" th:if="${#fields.hasErrors('description')}">Description Error</div>
<!-- Event Date -->
<label th:for="*{event_date}">Event Date</label>
<input type="date" th:field="*{event_date}">
<div class="alert alert-warning" th:errors="*{event_date}" th:if="${#fields.hasErrors('event_date')}">Date Error</div>
<!-- Event Time -->
<label th:for="*{event_time}">Event Time</label>
<input type="time" th:field="*{event_time}">
<div class="alert alert-warning" th:errors="*{event_time}" th:if="${#fields.hasErrors('event_time')}">Time Error</div>
<!-- Location -->
<label th:for="*{location}">Location</label>
<input type="text" th:field="*{location}" placeholder="Enter the event location...">
<div class="alert alert-warning" th:errors="*{location}" th:if="${#fields.hasErrors('location')}">Location Error</div>
<!-- Image URL -->
<label th:for="*{imageUrl}">Image URL</label>
<input type="text" th:field="*{imageUrl}" placeholder="Enter the image URL...">
<div class="alert alert-warning" th:errors="*{imageUrl}" th:if="${#fields.hasErrors('imageUrl')}">Image URL Error</div>
<!-- Why Join -->
<label th:for="*{whyJoin}">Why Join</label>
<textarea th:field="*{whyJoin}" placeholder="Explain why someone should join..."></textarea>
<div class="alert alert-warning" th:errors="*{whyJoin}" th:if="${#fields.hasErrors('whyJoin')}">Why Join Error</div>
<!-- Benefits -->
<label th:for="*{benefits}">Benefits</label>
<textarea th:field="*{benefits}" placeholder="List the benefits..."></textarea>
<div class="alert alert-warning" th:errors="*{benefits}" th:if="${#fields.hasErrors('benefits')}">Benefits Error</div>
<!-- Buttons -->
<div class="buttons">
<a href="#" id="cancelButton" class="button cancel">Cancel</a>
<button type="submit" class="submit">Add Event</button>
</div>
</form>
</section>
</html>
\ No newline at end of file
......@@ -25,7 +25,10 @@
<p><strong>Location:</strong> <span th:text="${event.getLocation()}">Event location</span></p>
</a>
</div>
<button class="register-button">Register Now</button>
<div class ="event-card-button">
<button class="register-button">Register Now</button>
<a th:href="@{/event/editEvent/{id}(id=${event.getEvent_id()})}" class="event-link"><button>Edit Event</button></a>
</div>
</div>
</div>
</div>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment