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
9 changes: 9 additions & 0 deletions Docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM mcr.microsoft.com/openjdk/jdk:21-ubuntu

WORKDIR /app

COPY java.docker.day.2-0.0.1.jar /app/java.docker.day.2-0.0.1.jar

EXPOSE 4000

ENTRYPOINT [ "java", "-jar", "java.docker.day.2-0.0.1.jar" ]
21 changes: 21 additions & 0 deletions Docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
services:
app:
image: 'day2:latest'
container_name: app
depends_on:
- db
ports:
- '4000:4000'
environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/mypostgresuser
- SPRING_DATASOURCE_USERNAME=mypostgresuser
- SPRING_DATASOURCE_PASSWORD=mypostgrespassword
- SPRING_JPA_HIBERNATE_DDL_AUTO=update

db:
image: 'postgres:latest'
container_name: db
environment:
- POSTGRES_USER=mypostgresuser
- POSTGRES_DATABASE=mypostgresuser
- POSTGRES_PASSWORD=mypostgrespassword
Binary file added Docker/java.docker.day.2-0.0.1.jar
Binary file not shown.
12 changes: 12 additions & 0 deletions src/main/java/com/booleanuk/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.booleanuk;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}

Empty file.
55 changes: 55 additions & 0 deletions src/main/java/com/booleanuk/controllers/DepartmentController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.booleanuk.controllers;

import com.booleanuk.models.Department;
import com.booleanuk.repositories.DepartmentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;

import java.util.List;

@RestController
@RequestMapping("departments")
public class DepartmentController {
@Autowired
private DepartmentRepository departmentRepository;

@GetMapping
public ResponseEntity<List<Department>> getAllDepartments() {
return ResponseEntity.ok(this.departmentRepository.findAll());
}

@PostMapping
public ResponseEntity<Department> createDepartment(@RequestBody Department department) {
return new ResponseEntity<>(this.departmentRepository.save(department), HttpStatus.CREATED);
}

@GetMapping("{id}")
public ResponseEntity<Department> getDepartmentById(@PathVariable int id) {
Department department = this.departmentRepository.findById(id).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Did not find any department with the given id"));
return ResponseEntity.ok(department);
}

@PutMapping("{id}")
public ResponseEntity<Department> updateDepartment(@PathVariable int id, @RequestBody Department department) {
Department departmentToUpdate = this.departmentRepository.findById(id).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Did not find any department with the given id to update"));

departmentToUpdate.setName(department.getName());
departmentToUpdate.setLocation(department.getLocation());

return new ResponseEntity<>(this.departmentRepository.save(departmentToUpdate), HttpStatus.CREATED);
}

@DeleteMapping("{id}")
public ResponseEntity<Department> deleteDepartment(@PathVariable int id) {
Department departmentToDelete = this.departmentRepository.findById(id).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Did not find any department with the given id to delete"));

this.departmentRepository.delete(departmentToDelete);
return ResponseEntity.ok(departmentToDelete);
}
}
74 changes: 74 additions & 0 deletions src/main/java/com/booleanuk/controllers/EmployeeController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.booleanuk.controllers;

import com.booleanuk.models.Department;
import com.booleanuk.models.Employee;
import com.booleanuk.repositories.DepartmentRepository;
import com.booleanuk.repositories.EmployeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;

import java.util.List;

@RestController
@RequestMapping("employees")
public class EmployeeController {
@Autowired
private EmployeeRepository employeeRepository;

@Autowired
private DepartmentRepository departmentRepository;

@GetMapping
public ResponseEntity<List<Employee>> getAllEmployees() {
return ResponseEntity.ok(this.employeeRepository.findAll());
}

@PostMapping("{id}")
public ResponseEntity<Employee> createEmployee(@RequestBody Employee employee, @PathVariable int id) {
Department department = departmentRepository.findById(id)
.orElseThrow(() -> new RuntimeException("Department not found"));

employee.setDepartment(department);
return new ResponseEntity<>(this.employeeRepository.save(employee), HttpStatus.CREATED);
}

@GetMapping("{id}")
public ResponseEntity<Employee> getUserById(@PathVariable int id) {
Employee employee = this.employeeRepository.findById(id).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Did not find any employee with the given id"));
return ResponseEntity.ok(employee);
}

@PutMapping("{id}")
public ResponseEntity<Employee> updateEmployee(@PathVariable int id, @RequestBody Employee employee) {
Employee employeeToUpdate = this.employeeRepository.findById(id).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Did not find any employee with the given id to update"));

employeeToUpdate.setFirstName(employee.getFirstName());
employeeToUpdate.setLastName(employee.getLastName());

int departmentId = employee.getDepartment().getId();
Department department = departmentRepository.findById(departmentId).orElseThrow(
() -> new ResponseStatusException(HttpStatus.BAD_REQUEST, "Department not found"));

employeeToUpdate.setDepartment(department);


employeeToUpdate.setDepartment(employee.getDepartment());

return new ResponseEntity<>(this.employeeRepository.save(employeeToUpdate), HttpStatus.CREATED);
}

@DeleteMapping("{id}")
public ResponseEntity<Employee> deleteEmployee(@PathVariable int id) {
Employee employeeToDelete = this.employeeRepository.findById(id).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Did not find any employee with the given id to delete"));

this.employeeRepository.delete(employeeToDelete);
return ResponseEntity.ok(employeeToDelete);
}

}
37 changes: 37 additions & 0 deletions src/main/java/com/booleanuk/models/Department.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.booleanuk.models;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.List;

@Getter
@Setter
@Entity
@NoArgsConstructor
@Table(name = "departments")
public class Department {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

@Column
private String name;

@Column
private String location;


@OneToMany(mappedBy = "department", fetch = FetchType.EAGER)
@JsonIgnoreProperties({"department"})
private List<Employee> employees;

public Department(String name, String location) {
this.name = name;
this.location = location;
}
}
36 changes: 36 additions & 0 deletions src/main/java/com/booleanuk/models/Employee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.booleanuk.models;

import com.fasterxml.jackson.annotation.JsonIncludeProperties;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
@Entity
@Table(name = "employees")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

@Column
private String firstName;

@Column
private String lastName;

@ManyToOne
//@JsonIgnoreProperties({"employee"})
@JsonIncludeProperties(value = {"name", "location"})
@JoinColumn(name = "department_id", nullable = false)
private Department department;

public Employee(String firstName, String lastName, Department department) {
this.firstName = firstName;
this.lastName = lastName;
this.department = department;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.booleanuk.repositories;


import com.booleanuk.models.Department;
import org.springframework.data.jpa.repository.JpaRepository;

public interface DepartmentRepository extends JpaRepository<Department, Integer> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.booleanuk.repositories;

import com.booleanuk.models.Employee;
import org.springframework.data.jpa.repository.JpaRepository;

public interface EmployeeRepository extends JpaRepository<Employee, Integer> {

}