Feature 1: Sign Up Page [Backend: New Attributes for the Person Class (5/23); Frontend: Sign Up Page (5/25); Backend: Connect the new users generated after they sign up to the database (5/26)]
The Frontend operations for the sign up
- This code is what allows to add people to the database. Whenever we click sign up, it will allow the person to be created.
- The ‘/api/person/post’ is what allows the user to create their account into our database.
fetch(fetchUrl + '/api/person/post', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(requestBody) }) .then(response => { if (!response.ok) { throw new Error('Network response was not ok'); } return response.text().then(text => { // Try to parse the response as JSON try { return JSON.parse(text); } catch (error) { // If parsing fails, return the text as is return text; } }); }) .then(data => { console.log(data); }) .catch(error => { console.error('There was a problem with your fetch operation:', error); }); window.location.replace("/CSABlog/sign-in/"); </script>
The backend operations
The Person request class: The PersonRequest class encapsulates the data needed to create or update a Person object, including email, password, name, USN, and subjects of interest, and provides a constructor to initialize these fields along with getter methods to retrieve their values. This class is typically used to transfer data between client and server in a structured manner.
package com.nighthawk.spring_portfolio.mvc.person;
public class PersonRequest {
private String email;
private String password;
private String name;
private String usn;
private String[] subjectsOfInterest;
public PersonRequest(String email, String password, String name, String usn, String[] subjectsOfInterest) {
this.email = email;
this.password = password;
this.name = name;
this.usn = usn;
this.subjectsOfInterest = subjectsOfInterest;
}
// getters and setters
public String getEmail() {
return this.email;
}
public String getPassword() {
return this.password;
}
public String getName() {
return this.name;
}
public String getUsn() {
return this.usn;
}
public String[] getSubjectsOfInterest() {
return this.subjectsOfInterest;
}
}
The Post method allows creation of a new person object that the user would eventually use. This method allows new users to be added to the database. It specifically adds new users to the database in addition to the subjects of interests. The subjects of interests and people will be placed in two separate sections but the same id is assigned to these sets of info. This will come in handy later on.
//This essentially creates a new person
@PostMapping("/post")
public ResponseEntity<Object> postPerson(@RequestBody PersonRequest personRequest) {
// A person object WITHOUT ID will create a new record with default roles as
// student
Person person = new Person(personRequest.getEmail(), personRequest.getPassword(), personRequest.getName(),
personRequest.getUsn(), personRequest.getSubjectsOfInterest()); // This assigns the user the basic attributes they requested
personDetailsService.save(person);
return new ResponseEntity<>(personRequest.getEmail() + " is created successfully", HttpStatus.CREATED);
}
//This creates a new separate collection of subjects assigned with the person_id. This is trying to directly add a new method/constructor to person class will make the backend not functon properly
@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "person_subjects", joinColumns = @JoinColumn(name = "person_id"))
@Column(name = "subject")
private Collection<String> subjectsOfInterest = new ArrayList<>();
FEATURE 2: THE CLASS CREATION FEATURE
- This will look for the people with the subjects of interest inside the database. This in turn will allow the users to add specific users to the classes.
```java
@GetMapping(“/getBySubject/{subjectOfInterest}”)
public ResponseEntity<?> getPersonsBySubject(@PathVariable String subjectOfInterest) {
List
personList = personDetailsService.getPersonsBySubjectOfInterest(subjectOfInterest); // regardless of outcome, even if it's an empty list, it's still a valid output return new ResponseEntity<>(personList, HttpStatus.OK); }
|
|
V
```java
//This method from the personDetailsService allows finding and getting the subjects of interest.
public List<Person> getPersonsBySubjectOfInterest(String subjectOfInterest) {
return personJpaRepository.findBySubjectOfInterestIgnoreCase(subjectOfInterest);
}
| | V
// List<Person> findBySubjectsOfInterestContainingIgnoreCase(String subjectOfInterest);
// CUSTOM QUERY METHOD TO GET A SUBJECT OF INTEREST
@Query("SELECT p FROM Person p JOIN p.subjectsOfInterest s WHERE LOWER(s) = LOWER(:subjectOfInterest)")
List<Person> findBySubjectOfInterestIgnoreCase(@Param("subjectOfInterest") String subjectOfInterest);
FEATURE 3: EXTRA - STACKS AND QUEUES (UNDO AND REDO)
- I utilized the undo and redo stacks so we can perform basic operations like undo and redo in case we accidentally added students
- Whenever I add a student, this action is added to the undo stack.
- In case I unintentionally added an irrelevant student, I can click the undo button to ensure that irrelevant student is not in that class
- “Oh I accidentally undid adding a student” I can just click the redo button to reverse the action
- Whenever I add another student after undoing, the redo stack just empties
```package com.nighthawk.spring_portfolio.mvc.classPeriod;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import java.util.Stack; import java.util.LinkedList; import java.util.Queue;
@RestController
public class UndoRedo {
private Stack
public UndoRedo() {
undoStack = new Stack<>();
redoStack = new Stack<>();
actionsQueue = new LinkedList<>();
}
@PostMapping("/addAction")
public String addAction(@RequestBody ActionData actionData) {
undoStack.push(actionData);
redoStack.clear(); // Clear redo stack on new action
actionsQueue.add(actionData);
return "Action added: " + actionData.getType(); // Example: Assuming ActionData has a getType() method
}
@GetMapping("/undo")
public String undo() {
if (!undoStack.isEmpty()) {
ActionData actionData = undoStack.pop();
redoStack.push(actionData);
return "Undo action: " + actionData.getType();
}
return "No actions to undo";
}
@GetMapping("/redo")
public String redo() {
if (!redoStack.isEmpty()) {
ActionData actionData = redoStack.pop();
undoStack.push(actionData);
return "Redo action: " + actionData.getType();
}
return "No actions to redo";
}
@GetMapping("/getActionsQueue")
public Queue<ActionData> getActionsQueue() {
return actionsQueue;
} } ``` ## My Contributions ![image](https://github.com/John-sCC/jcc_backend/assets/75040379/4780ea40-e49d-4322-b323-0010a33c93cd)