Skip to content

Commit e8e7ca7

Browse files
Uptil now, I've made the equipment and admin login table and Worked on Employee and classes, plus the many to many relationship they have, established a mid table, all respective repositories plus controllers except for the EmployeeClass Joint entity, and used cascading type all hibernate ensures automatic deletion in such regard. Next step, might be to build the missing controller or just deal with this functionality from front end basis.
1 parent 842e644 commit e8e7ca7

24 files changed

+927
-2
lines changed

‎pom.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,25 @@
5252
<artifactId>spring-boot-starter-test</artifactId>
5353
<scope>test</scope>
5454
</dependency>
55+
<!-- Spring Boot Starter Web for creating REST APIs -->
56+
<dependency>
57+
<groupId>org.springframework.boot</groupId>
58+
<artifactId>spring-boot-starter-web</artifactId>
59+
</dependency>
60+
61+
<!-- Spring Boot Starter Data JPA for database access -->
62+
<dependency>
63+
<groupId>org.springframework.boot</groupId>
64+
<artifactId>spring-boot-starter-data-jpa</artifactId>
65+
</dependency>
66+
67+
<!-- Your database connector, e.g., MySQL -->
68+
<dependency>
69+
<groupId>com.mysql</groupId>
70+
<artifactId>mysql-connector-j</artifactId>
71+
<scope>runtime</scope>
72+
</dependency>
73+
5574
</dependencies>
5675

5776
<build>

‎src/main/java/com/example/easynotes/EasyNotesApplication.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ public class EasyNotesApplication {
1111
public static void main(String[] args) {
1212
SpringApplication.run(EasyNotesApplication.class, args);
1313
}
14+
15+
1416
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.example.easynotes.controller;
2+
3+
import com.example.easynotes.model.AdminLogin;
4+
import com.example.easynotes.model.Person;
5+
import com.example.easynotes.repository.AdminLoginRepository;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.http.ResponseEntity;
8+
import org.springframework.web.bind.annotation.*;
9+
import com.example.easynotes.exception.ResourceNotFoundException;
10+
import java.util.Map;
11+
import java.util.HashMap;
12+
import java.util.Optional;
13+
import org.springframework.web.bind.annotation.*;
14+
import java.util.List;
15+
import java.util.Optional;
16+
17+
@RestController
18+
@RequestMapping("/api/admins")
19+
public class AdminLoginController {
20+
21+
@Autowired
22+
private AdminLoginRepository adminLoginRepository;
23+
24+
@GetMapping
25+
public List<AdminLogin> getAllAdmins() {
26+
return adminLoginRepository.findAll();
27+
}
28+
29+
@PostMapping
30+
public AdminLogin createAdmin(@RequestBody AdminLogin adminLogin) {
31+
return adminLoginRepository.save(adminLogin);
32+
}
33+
34+
@GetMapping("/{id}")
35+
public ResponseEntity<AdminLogin> getAdminById(@PathVariable Long id) {
36+
AdminLogin adminLogin = adminLoginRepository.findById(id)
37+
.orElseThrow(() -> new ResourceNotFoundException("AdminLogin", "adminID", id));
38+
39+
return ResponseEntity.ok().body(adminLogin);
40+
}
41+
42+
@PutMapping("/{id}")
43+
public ResponseEntity<AdminLogin> updateAdmin(@PathVariable Long id, @RequestBody AdminLogin adminDetails) {
44+
AdminLogin adminLogin = adminLoginRepository.findById(id)
45+
.orElseThrow(() -> new ResourceNotFoundException("AdminLogin", "adminID", id));
46+
47+
48+
adminLogin.setPassword(adminDetails.getPassword());
49+
50+
final AdminLogin updatedAdmin = adminLoginRepository.save(adminLogin);
51+
return ResponseEntity.ok(updatedAdmin);
52+
}
53+
54+
@DeleteMapping("/{id}")
55+
public Map<String, Boolean> deleteAdmin(@PathVariable Long id) {
56+
AdminLogin adminLogin = adminLoginRepository.findById(id)
57+
.orElseThrow(() -> new ResourceNotFoundException("AdminLogin", "adminID", id));
58+
59+
60+
adminLoginRepository.delete(adminLogin);
61+
Map<String, Boolean> response = new HashMap<>();
62+
response.put("deleted", Boolean.TRUE);
63+
return response;
64+
}
65+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.example.easynotes.controller;
2+
3+
import com.example.easynotes.model.Classes;
4+
import com.example.easynotes.repository.ClassesRepository;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.*;
7+
8+
import java.util.List;
9+
import java.util.Optional;
10+
11+
@RestController
12+
@RequestMapping("/api/classes")
13+
public class ClassesController {
14+
15+
@Autowired
16+
private ClassesRepository classesRepository;
17+
18+
// GET all classes
19+
@GetMapping
20+
public List<Classes> getAllClasses() {
21+
return classesRepository.findAll();
22+
}
23+
24+
// GET class by ID
25+
@GetMapping("/{id}")
26+
public Optional<Classes> getClassById(@PathVariable long id) {
27+
return classesRepository.findById(id);
28+
}
29+
30+
// POST create a new class
31+
@PostMapping
32+
public Classes createClass(@RequestBody Classes classes) {
33+
return classesRepository.save(classes);
34+
}
35+
36+
// PUT update an existing class
37+
@PutMapping("/{id}")
38+
public Classes updateClass(@PathVariable long id, @RequestBody Classes classDetails) {
39+
return classesRepository.findById(id)
40+
.map(classes -> {
41+
classes.setClassName(classDetails.getClassName());
42+
classes.setClassDescription(classDetails.getClassDescription());
43+
classes.setClassSchedule(classDetails.getClassSchedule());
44+
classes.setMaximumCapacity(classDetails.getMaximumCapacity());
45+
return classesRepository.save(classes);
46+
}).orElseThrow(() -> new RuntimeException("Class not found"));
47+
}
48+
49+
// DELETE a class
50+
@DeleteMapping("/{id}")
51+
public void deleteClass(@PathVariable long id) {
52+
classesRepository.deleteById(id);
53+
}
54+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.example.easynotes.controller;
2+
3+
import com.example.easynotes.model.Employee;
4+
import com.example.easynotes.repository.ClassesRepository;
5+
import com.example.easynotes.repository.EmployeeRepository;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.web.bind.annotation.*;
8+
import com.example.easynotes.repository.EmployeeClassRepository;
9+
10+
import java.util.List;
11+
import java.util.Optional;
12+
13+
@RestController
14+
@RequestMapping("/api/employees")
15+
public class EmployeeController {
16+
17+
@Autowired
18+
private EmployeeRepository employeeRepository;
19+
20+
@Autowired
21+
private ClassesRepository classesRepository;
22+
23+
@Autowired
24+
private EmployeeClassRepository employeeClassRepository;
25+
26+
// GET all employees
27+
@GetMapping
28+
public List<Employee> getAllEmployees() {
29+
return employeeRepository.findAll();
30+
}
31+
32+
// GET employee by ID
33+
@GetMapping("/{id}")
34+
public Optional<Employee> getEmployeeById(@PathVariable Integer id) {
35+
return employeeRepository.findById(id);
36+
}
37+
38+
// POST create a new employee
39+
@PostMapping
40+
public Employee createEmployee(@RequestBody Employee employee) {
41+
return employeeRepository.save(employee);
42+
}
43+
44+
// PUT update an existing employee
45+
@PutMapping("/{id}")
46+
public Employee updateEmployee(@PathVariable Integer id, @RequestBody Employee employeeDetails) {
47+
return employeeRepository.findById(id)
48+
.map(employee -> {
49+
employee.setName(employeeDetails.getName());
50+
employee.setDateOfBirth(employeeDetails.getDateOfBirth());
51+
employee.setContactNumber(employeeDetails.getContactNumber());
52+
employee.setEmailAddress(employeeDetails.getEmailAddress());
53+
employee.setAddress(employeeDetails.getAddress());
54+
employee.setHireDate(employeeDetails.getHireDate());
55+
employee.setEmployeeStatus(employeeDetails.getEmployeeStatus());
56+
employee.setPassword(employeeDetails.getPassword());
57+
return employeeRepository.save(employee);
58+
}).orElseThrow(() -> new RuntimeException("Employee not found"));
59+
}
60+
61+
// DELETE an employee
62+
@DeleteMapping("/{id}")
63+
public void deleteEmployee(@PathVariable Integer id) {
64+
employeeRepository.deleteById(id);
65+
}
66+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.example.easynotes.controller;
2+
3+
import com.example.easynotes.exception.ResourceNotFoundException;
4+
import com.example.easynotes.model.Equipment;
5+
import com.example.easynotes.model.Note;
6+
import com.example.easynotes.model.Person;
7+
import com.example.easynotes.repository.EquipmentRepository;
8+
import com.example.easynotes.repository.NoteRepository;
9+
import com.example.easynotes.repository.PersonRepository;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.http.ResponseEntity;
12+
import org.springframework.web.bind.annotation.*;
13+
14+
import javax.validation.Valid;
15+
import java.util.List;
16+
import java.util.Optional;
17+
18+
@RestController
19+
@RequestMapping("/api/equipment")
20+
public class EquipmentController {
21+
22+
@Autowired
23+
private EquipmentRepository equipmentRepository;
24+
25+
@GetMapping
26+
public List<Equipment> getAllEquipment() {
27+
return equipmentRepository.findAll();
28+
29+
}
30+
31+
// GET person by ID
32+
33+
@GetMapping("/{id}")
34+
public Optional<Equipment> getEquipmentById(@PathVariable Long id) {
35+
return equipmentRepository.findById(id);
36+
}
37+
38+
@PostMapping
39+
public Equipment createEquipment(@RequestBody Equipment equipment) {
40+
return equipmentRepository.save(equipment);
41+
}
42+
43+
@DeleteMapping("/{id}")
44+
public String deleteEquipment(@PathVariable Long id) {
45+
equipmentRepository.deleteById(id);
46+
return "Equipment with ID: " + id + " has been deleted from the list of equipments ";
47+
}
48+
49+
50+
// PUT endpoint to update equipment
51+
@PutMapping("/{id}")
52+
public ResponseEntity<Equipment> updateEquipment(
53+
@PathVariable(value = "id") Long equipmentId,
54+
@Valid @RequestBody Equipment equipmentDetails) {
55+
56+
// Find the equipment by ID
57+
Equipment equipment = equipmentRepository.findById(equipmentId)
58+
.orElseThrow(() -> new ResourceNotFoundException("Equipment not found for this id :: ", "equipment id:", + equipmentId));
59+
60+
// Update the equipment details
61+
equipment.setEquipmentName(equipmentDetails.getEquipmentName());
62+
equipment.setEquipmentDescription(equipmentDetails.getEquipmentDescription());
63+
equipment.setEquipmentType(equipmentDetails.getEquipmentType());
64+
equipment.setPurchaseDate(equipmentDetails.getPurchaseDate());
65+
equipment.setPurchasePrice(equipmentDetails.getPurchasePrice());
66+
equipment.setCurrentStatus(equipmentDetails.getCurrentStatus());
67+
68+
// Save the updated equipment
69+
final Equipment updatedEquipment = equipmentRepository.save(equipment);
70+
return ResponseEntity.ok(updatedEquipment);
71+
}
72+
73+
}

‎src/main/java/com/example/easynotes/controller/NoteController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public Note updateNote(@PathVariable(value = "id") Long noteId,
4545

4646
note.setTitle(noteDetails.getTitle());
4747
note.setContent(noteDetails.getContent());
48-
48+
note.setExtra(noteDetails.getExtra());
4949
Note updatedNote = noteRepository.save(note);
5050
return updatedNote;
5151
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.example.easynotes.controller;
2+
import com.example.easynotes.model.Person;
3+
import com.example.easynotes.repository.PersonRepository;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.web.bind.annotation.*;
6+
import com.example.easynotes.model.Address;
7+
import java.util.List;
8+
import java.util.Optional;
9+
10+
@RestController
11+
@RequestMapping("/api/persons")
12+
public class PersonController {
13+
@Autowired
14+
private PersonRepository personRepository;
15+
16+
@GetMapping
17+
public List<Person> getAllPersons() {
18+
return personRepository.findAll();
19+
20+
}
21+
22+
;
23+
// GET person by ID
24+
25+
@GetMapping("/{id}")
26+
public Optional<Person> getPersonById(@PathVariable Long id) {
27+
return personRepository.findById(id);
28+
}
29+
30+
@PostMapping
31+
public Person createPerson(@RequestBody Person person) {
32+
return personRepository.save(person);
33+
}
34+
35+
@PutMapping("/{id}")
36+
public Person updatePerson(@PathVariable Long id, @RequestBody Person personDetails) {
37+
return personRepository.findById(id).map(person -> {
38+
person.setName(personDetails.getName());
39+
person.setAge(personDetails.getAge());
40+
41+
Address address = person.getAddress();
42+
if (address != null && personDetails.getAddress() != null) {
43+
address.setStreet(personDetails.getAddress().getStreet());
44+
address.setCity(personDetails.getAddress().getCity());
45+
address.setState(personDetails.getAddress().getState());
46+
address.setZipcode(personDetails.getAddress().getZipcode());
47+
person.setAddress(address); // Update address relationship
48+
}
49+
50+
return personRepository.save(person);
51+
52+
}).orElseThrow(() -> new RuntimeException("Person not found with id " + id));
53+
54+
55+
56+
}
57+
58+
@DeleteMapping("/{ID}")
59+
public String deletePerson(@PathVariable Long ID) {
60+
personRepository.deleteById(ID);
61+
return "Person with ID: " + ID + " has been deleted from the list of persons ";
62+
}
63+
}

0 commit comments

Comments
 (0)