Commit cb9808e4 authored by s7_spruge_k's avatar s7_spruge_k
Browse files

Merge branch 'database' into 'master'

Database

See merge request s7_spruge_k/calendar!5
parents 65374936 cd98dcf0
File added
......@@ -27,7 +27,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
......@@ -38,6 +41,15 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
......
......@@ -2,24 +2,19 @@ package com.example.calendar;
import java.sql.Date;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import com.example.models.Event;
import com.example.models.EventType;
@SpringBootApplication
@EnableJpaRepositories
public class CalendarApplication {
public static void main(String[] args) {
SpringApplication.run(CalendarApplication.class, args);
System.out.println("Event test");
Date startingDate = new Date(2019, 05, 11);
Date endingDate = new Date(2019, 05, 12);
}
......
package com.example.calendar.controller;
import java.time.LocalDate;
import java.time.LocalTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import com.example.calendar.models.Calendar;
import com.example.calendar.models.Event;
import com.example.calendar.models.User;
import com.example.calendar.repo.CalendarRepo;
import com.example.calendar.repo.EventRepo;
import com.example.calendar.repo.UserRepo;
@Controller
public class DBController {
@Autowired
UserRepo userRepo;
@Autowired
EventRepo eventRepo;
@Autowired
CalendarRepo calendarRepo;
@GetMapping(value="/ok")
public String test()
{
User u1 = new User("Janis", "parole123");
userRepo.save(u1);
Event e1 = new Event("Test Event", LocalDate.now().plusDays(1), LocalTime.now().plusHours(1), false);
eventRepo.save(e1);
Calendar c1 = new Calendar();
//c1.addNewEvent(e1);
calendarRepo.save(c1);
//
System.out.println(userRepo.findAll());
System.out.println(eventRepo.findAll());
// System.out.println(calendarRepo.findAll());
return "ok";
}
@GetMapping(value="/getallevents")
public String getAllEvents(Model model){
//model.addAttribute("list", eventRepo.findAll());
System.out.println("---------------------");
System.out.println(eventRepo.findAll());
return "ok";
}
}
package com.example.models;
package com.example.calendar.models;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name = "CalendarTable")
public class Calendar {
//TODO A lot of functions should be made in the controller with the help of a database
//e.g. deleteEventByName(), getEventsByName() <-- not needed.
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID_c")
private int calendar_ID; //Auto generated with JPA?
//TODO all calendar arrayList should not be in Calendar class.
//TODO Do we need eventList? maybe link events&calendar as oneToMany
private ArrayList<Event> eventList = new ArrayList<>();
@OneToOne
@JoinColumn(name="ID_u")
private User user;
@OneToMany(mappedBy = "calendar")
private Collection<Event> event;
public Calendar() {
}
public Calendar(ArrayList<Event> eventList) {
setEventList(eventList);
}
public ArrayList<Event> getEventList() {
return eventList;
}
public void setEventList(ArrayList<Event> eventList) {
this.eventList = eventList;
}
//TODO DISCUSS We have 3 different constructors, which one to use here?
public boolean addNewEvent(String name, String description, LocalDate startDate, LocalTime startTime, EventType newEventType, boolean isAllDayEvent)
//TODO verification
public boolean addNewEvent(Event newEvent)
{
Event tempEvent = new Event(name, description, startDate, startTime, isAllDayEvent);
eventList.add(tempEvent);
event.add(newEvent);
return true;
}
//MADE THROUGH CONTROLLER PROBABLY
......@@ -61,7 +62,7 @@ public class Calendar {
if(eventsToDelete.size() == 1) //So only 1 event with the name matches
{
for (Event event : eventList) {
for (Event event : event) {
if(event.equals(eventsToDelete.get(0))) //We find the event and delete it
{
event = null;
......@@ -97,7 +98,7 @@ public class Calendar {
public ArrayList<Event> getEventsByName(String name)
{
ArrayList<Event> allEventsWithEqualName = new ArrayList<>();
for (Event e : eventList) {
for (Event e : event) {
if(e.getName().equals(name))
{
allEventsWithEqualName.add(e);
......@@ -114,10 +115,10 @@ public class Calendar {
if(eventToDelete != null)
{
for (Event e : eventList) {
for (Event e : event) {
if(e.getEvent_ID() == id)
{
eventList.remove(e);
event.remove(e);
}
}
return true;
......@@ -142,7 +143,7 @@ public class Calendar {
public Event getEventsByID(int id)
{
Event eventWithTheID = null;
for (Event e : eventList) {
for (Event e : event) {
if(e.getEvent_ID() == id)
{
eventWithTheID = e;
......@@ -163,7 +164,7 @@ public class Calendar {
public ArrayList<Event> getEventsByDate(LocalDate date)
{
ArrayList<Event> allEventsAtDate = new ArrayList<>();
for (Event e : eventList) {
for (Event e : event) {
if(e.getStartDate().equals(date))
{
allEventsAtDate.add(e);
......@@ -185,7 +186,7 @@ public class Calendar {
public ArrayList<Event> getEventsByEventType(EventType type)
{
ArrayList<Event> allEventsByType = new ArrayList<>();
for (Event e : eventList) {
for (Event e : event) {
if(e.getEventType().equals(type))
{
allEventsByType.add(e);
......@@ -196,8 +197,9 @@ public class Calendar {
@Override
public String toString() {
return "Calendar [calendar_ID=" + calendar_ID + ", eventList=" + eventList + "]";
return "Calendar [calendar_ID=" + calendar_ID + ", eventList=" + event + "]";
}
......
package com.example.models;
package com.example.calendar.models;
import java.time.LocalDate;
import java.time.LocalTime;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "EventTable")
public class Event {
@Column(name = "Name")
private String name;
@Column(name = "Description")
private String description;
@Column(name = "StartDate")
private LocalDate startDate;
@Column(name = "StartTime")
private LocalTime startTime;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID_e")
private int event_ID; //Auto-generated with JPA
@Column(name = "EventType")
private EventType eventType;
@Column(name = "IsAllDayEvent")
private boolean isAllDayEvent;
@ManyToOne
@JoinColumn(name = "ID_c")
private Calendar calendar;
......@@ -136,6 +158,10 @@ public class Event {
+ "]";
}
public Event() {
super();
}
}
package com.example.models;
package com.example.calendar.models;
public enum EventType {
......
package com.example.models;
package com.example.calendar.models;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name = "UserTable")
public class User {
//TODO spring secure auth
@Column(name = "Username")
private String username;
@Column(name = "Password")
private String password;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID_u")
private int user_ID; //Auto generated w/ jpa
@OneToOne(mappedBy = "user")
private Calendar calendar;
public User(String username, String password) {
setUsername(username);
......
package com.example.calendar.repo;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.example.calendar.models.Calendar;
@Repository
public interface CalendarRepo extends CrudRepository<Calendar, Integer>{
}
package com.example.calendar.repo;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.example.calendar.models.Event;
@Repository
public interface EventRepo extends CrudRepository<Event, Integer> {
}
package com.example.calendar.repo;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.example.calendar.models.User;
@Repository
public interface UserRepo extends CrudRepository<User, Integer>{
}
spring.datasource.url=jdbc:h2:file:./db
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=root
spring.datasource.password=parole123
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=update
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h3>OK!</h3>
</body>
</html>
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment