Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Us006 post/put/delete event #1

Merged
merged 12 commits into from
Nov 23, 2023
8 changes: 6 additions & 2 deletions .github/workflows/main_api-onav.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
with:
name: java-app
path: '${{ github.workspace }}/target/*.jar'

deploy:
runs-on: ubuntu-latest
needs: build
Expand All @@ -50,4 +50,8 @@ jobs:
app-name: 'api-onav'
slot-name: 'Production'
package: '*.jar'
publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_6501A6378CF94AB893EE435AA51B5D2F }}
publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_6501A6378CF94AB893EE435AA51B5D2F }}
env:
DATABASE_URL: ${{ secrets.DATABASE_URL }}
DATABASE_USERNAME: ${{ secrets.DATABASE_USERNAME }}
DATABASE_PASSWORD: ${{ secrets.DATABASE_PASSWORD }}
13 changes: 9 additions & 4 deletions .github/workflows/pr_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Maven Build and Test

on:
pull_request:
branches: [ main ]
branches: [main]

jobs:
build:
Expand All @@ -11,14 +11,19 @@ jobs:
- name: Checkout
uses: actions/checkout@v3

- name: Set up JDK 11
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
distribution: 'adopt'
java-version: '11'
java-version: '17'

- name: Build with Maven
run: mvn -B package --file pom.xml

- name: Test with Maven
run: mvn test
run: mvn test

env:
DATABASE_URL: ${{ secrets.DATABASE_URL }}
DATABASE_USERNAME: ${{ secrets.DATABASE_USERNAME }}
DATABASE_PASSWORD: ${{ secrets.DATABASE_PASSWORD }}
26 changes: 26 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,31 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.5.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand All @@ -65,6 +83,14 @@
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version> <!-- Use the latest version that supports Java 17 -->
<configuration>
<release>17</release>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
50 changes: 50 additions & 0 deletions src/main/java/dk/kea/onav2ndproject_rest/api/EventController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package dk.kea.onav2ndproject_rest.api;

import dk.kea.onav2ndproject_rest.dto.EventDTO;
import dk.kea.onav2ndproject_rest.entity.Event;
import dk.kea.onav2ndproject_rest.service.EventService;
import org.apache.coyote.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/api/event")
public class EventController {

@Autowired
private EventService eventService;

@GetMapping
public Page<EventDTO> getAllEvents(Pageable pageable) {
return eventService.getAllEvents(pageable);
}

@GetMapping("/{id}")
public ResponseEntity<EventDTO> getEventById(@PathVariable int id) {
EventDTO event = eventService.getEventById(id);
return new ResponseEntity<>(event, HttpStatus.OK);
}

@PostMapping
public ResponseEntity<EventDTO> createEvent(@RequestBody EventDTO eventDTO) {
EventDTO createdEvent = eventService.createEvent(eventDTO);
return new ResponseEntity<>(createdEvent, HttpStatus.CREATED);
}

@PutMapping("/{id}")
public ResponseEntity<EventDTO> updateEvent(@PathVariable int id, @RequestBody EventDTO eventDTO) {
EventDTO updatedEvent = eventService.updateEvent(id, eventDTO);
return new ResponseEntity<>(updatedEvent, HttpStatus.OK);
}

@DeleteMapping("/{id}")
public ResponseEntity<String> deleteEvent(@PathVariable int id) {
eventService.deleteEventById(id);
return new ResponseEntity<>("Event with id " + id + " was deleted", HttpStatus.OK);
}
}
32 changes: 32 additions & 0 deletions src/main/java/dk/kea/onav2ndproject_rest/dto/EventConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package dk.kea.onav2ndproject_rest.dto;

import dk.kea.onav2ndproject_rest.entity.Event;
import dk.kea.onav2ndproject_rest.repository.EventRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class EventConverter {
public Event toEntity(EventDTO eventDTO) {
Event event = new Event();
event.setName(eventDTO.name());
event.setStartDate(eventDTO.startDate());
event.setEndDate(eventDTO.endDate());
event.setDescription(eventDTO.description());
event.setLocation(eventDTO.location());
event.setImgRef(eventDTO.imgRef());
return event;
}

public EventDTO toDTO(Event event) {
return new EventDTO(
event.getId(),
event.getName(),
event.getStartDate(),
event.getEndDate(),
event.getDescription(),
event.getLocation(),
event.getImgRef()
);
}
}
6 changes: 6 additions & 0 deletions src/main/java/dk/kea/onav2ndproject_rest/dto/EventDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package dk.kea.onav2ndproject_rest.dto;

import java.time.LocalDate;

public record EventDTO(int id, String name, LocalDate startDate, LocalDate endDate, String description, String location, String imgRef) {
}
7 changes: 7 additions & 0 deletions src/main/java/dk/kea/onav2ndproject_rest/dto/UserDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dk.kea.onav2ndproject_rest.dto;

import dk.kea.onav2ndproject_rest.entity.Department;
import dk.kea.onav2ndproject_rest.entity.Role;

public record UserDTO(String name, String username, Role role, String email, Boolean notifications, Department department) {
}
26 changes: 26 additions & 0 deletions src/main/java/dk/kea/onav2ndproject_rest/entity/Department.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package dk.kea.onav2ndproject_rest.entity;

import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.Set;

@Getter
@Setter
@NoArgsConstructor
@Entity
public class Department {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
@ManyToMany
@JoinTable(
name="invitation",
joinColumns = {@JoinColumn(name = "department_id")},
inverseJoinColumns = {@JoinColumn(name = "event_id")}
)
private Set<Event> events;
}
30 changes: 30 additions & 0 deletions src/main/java/dk/kea/onav2ndproject_rest/entity/Event.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package dk.kea.onav2ndproject_rest.entity;


import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.time.LocalDate;
import java.util.Set;

@Getter
@Setter
@NoArgsConstructor
@Entity
public class Event {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private LocalDate startDate;
private LocalDate endDate;
private String description;
private String location;
private String imgRef;
@ManyToMany(mappedBy = "event")
private Set<User> users;
@ManyToMany(mappedBy = "events")
private Set<Department> departments;
}
5 changes: 5 additions & 0 deletions src/main/java/dk/kea/onav2ndproject_rest/entity/Role.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package dk.kea.onav2ndproject_rest.entity;

public enum Role {
MANAGER, EMPLOYEE, HEADCHEF
}
34 changes: 34 additions & 0 deletions src/main/java/dk/kea/onav2ndproject_rest/entity/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package dk.kea.onav2ndproject_rest.entity;

import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.Set;

@Getter
@Setter
@NoArgsConstructor
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String username;
private String password;
private Role role;
private String email;
private Boolean notications;
@ManyToOne
@JoinColumn(name = "department_id")
private Department department;
@ManyToMany
@JoinTable(
name="user_event",
joinColumns = {@JoinColumn(name = "user_id")},
inverseJoinColumns = {@JoinColumn(name = "event_id")}
)
private Set<Event> event;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dk.kea.onav2ndproject_rest.exeception;

public class EventNotFoundException extends RuntimeException {
public EventNotFoundException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dk.kea.onav2ndproject_rest.repository;

import dk.kea.onav2ndproject_rest.entity.Event;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface EventRepository extends JpaRepository<Event, Integer> {
}
64 changes: 64 additions & 0 deletions src/main/java/dk/kea/onav2ndproject_rest/service/EventService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package dk.kea.onav2ndproject_rest.service;

import dk.kea.onav2ndproject_rest.dto.EventConverter;
import dk.kea.onav2ndproject_rest.dto.EventDTO;
import dk.kea.onav2ndproject_rest.entity.Event;
import dk.kea.onav2ndproject_rest.exeception.EventNotFoundException;
import dk.kea.onav2ndproject_rest.repository.EventRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class EventService {
@Autowired
EventRepository eventRepository;
@Autowired
EventConverter eventConverter;

public Page<EventDTO> getAllEvents(Pageable pageable) {
Page<Event> events = eventRepository.findAll(pageable);
return events.map(eventConverter::toDTO);
}

public EventDTO getEventById(int id) {
Optional<Event> event = eventRepository.findById(id);
if (event.isPresent()) {
return eventConverter.toDTO(event.get());
} else {
throw new EventNotFoundException("Event does not exist" + id);
}
}

public EventDTO createEvent(EventDTO eventDTO) {
Event event = eventConverter.toEntity(eventDTO);
event.setId(0);

Event savedEvent = eventRepository.save(event);
return eventConverter.toDTO(savedEvent);
}

public EventDTO updateEvent(int id, EventDTO eventDTO){
Optional<Event> event = eventRepository.findById(id);
if (event.isPresent()){
Event eventToUpdate = eventConverter.toEntity(eventDTO);
eventToUpdate.setId(id);
Event updatedEvent = eventRepository.save(eventToUpdate);
return eventConverter.toDTO(updatedEvent);
} else {
throw new EventNotFoundException("Event does not exist" + id);
}
}

public void deleteEventById(int id){
Optional<Event> event = eventRepository.findById(id);
if (event.isPresent()){
eventRepository.deleteById(id);
} else {
throw new EventNotFoundException("Event does not exist" + id);
}
}
}
6 changes: 3 additions & 3 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
server.error.include-stacktrace=never
server.error.include-message=always
#spring.datasource.url=${DATABASE_URL}
#spring.datasource.username=${DATABASE_USERNAME}
#spring.datasource.password=${DATABASE_PASSWORD}
spring.datasource.url=${DATABASE_URL}
spring.datasource.username=${DATABASE_USERNAME}
spring.datasource.password=${DATABASE_PASSWORD}
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update