Get person from DB

This commit is contained in:
Araozu 2024-03-15 14:56:19 -05:00
parent 9f731c1c5c
commit a29e410332
10 changed files with 173 additions and 121 deletions

View File

@ -1,34 +0,0 @@
package dev.araozu.eeg_java.controller;
import dev.araozu.eeg_java.model.Person;
import dev.araozu.eeg_java.model.PersonRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@Controller
@RequestMapping(path = "/demo")
public class MainController {
@Autowired
private PersonRepository personRepository;
@PostMapping(path = "/add")
public @ResponseBody String addNewPerson(@RequestParam String name, @RequestParam String dni) {
Person person = new Person();
person.setPerson_dni(dni);
person.setPerson_names(name);
person.setPerson_paternal_surname("Doe");
person.setPerson_maternal_surname("Smith");
person.setPerson_classroom_id(1);
person.setPerson_classroom_username("JohnDoe");
personRepository.save(person);
return "Saved :D";
}
@GetMapping(path = "/all")
public @ResponseBody Iterable<Person> getAllPersons() {
return personRepository.findAll();
}
}

View File

@ -1,76 +0,0 @@
package dev.araozu.eeg_java.model;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
@Entity(name = "person")
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer person_id;
private String person_dni;
private String person_names;
private String person_paternal_surname;
private String person_maternal_surname;
private Integer person_classroom_id;
private String person_classroom_username;
public Integer getPerson_id() {
return person_id;
}
public void setPerson_id(Integer person_id) {
this.person_id = person_id;
}
public String getPerson_dni() {
return person_dni;
}
public void setPerson_dni(String person_dni) {
this.person_dni = person_dni;
}
public String getPerson_names() {
return person_names;
}
public void setPerson_names(String person_names) {
this.person_names = person_names;
}
public String getPerson_paternal_surname() {
return person_paternal_surname;
}
public void setPerson_paternal_surname(String person_paternal_surname) {
this.person_paternal_surname = person_paternal_surname;
}
public String getPerson_maternal_surname() {
return person_maternal_surname;
}
public void setPerson_maternal_surname(String person_maternal_surname) {
this.person_maternal_surname = person_maternal_surname;
}
public Integer getPerson_classroom_id() {
return person_classroom_id;
}
public void setPerson_classroom_id(Integer person_classroom_id) {
this.person_classroom_id = person_classroom_id;
}
public String getPerson_classroom_username() {
return person_classroom_username;
}
public void setPerson_classroom_username(String person_classroom_username) {
this.person_classroom_username = person_classroom_username;
}
}

View File

@ -0,0 +1,87 @@
package dev.araozu.eeg_java.person;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
@Entity(name = "person")
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer person_id;
@Column(name = "person_dni")
private String personDni;
@Column(name = "person_names")
private String personNames;
@Column(name = "person_paternal_surname")
private String personPaternalSurname;
@Column(name = "person_maternal_surname")
private String personMaternalSurname;
@Column(name = "person_classroom_id")
private Integer personClassroomId;
@Column(name = "person_classroom_username")
private String personClassroomUsername;
public String getPersonClassroomUsername() {
return personClassroomUsername;
}
public void setPersonClassroomUsername(String personClassroomUsername) {
this.personClassroomUsername = personClassroomUsername;
}
public Integer getPersonId() {
return person_id;
}
public void setPersonId(Integer person_id) {
this.person_id = person_id;
}
public String getPersonDni() {
return personDni;
}
public void setPersonDni(String person_dni) {
this.personDni = person_dni;
}
public String getPersonNames() {
return personNames;
}
public void setPersonNames(String personNames) {
this.personNames = personNames;
}
public String getPersonPaternalSurname() {
return personPaternalSurname;
}
public void setPersonPaternalSurname(String personPaternalSurname) {
this.personPaternalSurname = personPaternalSurname;
}
public String getPersonMaternalSurname() {
return personMaternalSurname;
}
public void setPersonMaternalSurname(String personMaternalSurname) {
this.personMaternalSurname = personMaternalSurname;
}
public Integer getPersonClassroomId() {
return personClassroomId;
}
public void setPersonClassroomId(Integer personClassroomId) {
this.personClassroomId = personClassroomId;
}
}

View File

@ -1,19 +1,39 @@
package dev.araozu.eeg_java.person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
@RequestMapping(path = "/person")
public class PersonController {
@Autowired
private PersonRepository personRepository;
@GetMapping("/")
public String searchPersonByDniString(
@RequestParam(required = true, value="person_dni") String personDni
@RequestParam(required = true, value="person_dni") String personDni,
HttpServletResponse response
) {
System.out.println("PersonController.searchPersonByDniString: " + personDni);
return "fragments/sample.html";
var maybePerson = personRepository.findByPersonDni(personDni);
if (maybePerson.isPresent()) {
Person person = maybePerson.get();
System.out.println(person.getPersonDni());
return "fragments/person/display.html";
} else {
// send http 404
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return "fragments/person/create.html";
}
}
}

View File

@ -1,6 +1,9 @@
package dev.araozu.eeg_java.model;
package dev.araozu.eeg_java.person;
import java.util.Optional;
import org.springframework.data.repository.CrudRepository;
public interface PersonRepository extends CrudRepository<Person, Integer> {
Optional<Person> findByPersonDni(String personDni);
}

View File

@ -10,7 +10,9 @@
<!-- Certs -->
<div>
<div class="grid grid-cols-[16rem_25rem_1fr]">
<div class="grid grid-cols-[16rem_25rem_1fr]"
x-data="{user_dni: ''}"
>
<!-- Search -->
<div>
<div class="text-center">
@ -19,7 +21,7 @@
<img class="inline-block w-[10rem] h-[10rem]" src="" />
</div>
</div>
<div x-data="{user_dni: ''}">
<div>
<form
hx-get="/person/" hx-target="#person-search-result"
hx-target-4*="#person-search-result"
@ -34,6 +36,7 @@
maxLength="15" pattern="[0-9]{8,15}" placeholder="Número de DNI" x-model="user_dni"
required />
<label for="search-dni"
class="absolute -top-2 left-2 text-xs bg-c-surface px-1 select-none">DNI</label>
@ -55,7 +58,7 @@
</button>
</form>
<p class="relative max-w-[14rem] mx-auto p-1 text-c-error text-sm select-none opacity-0">
<p class="relative max-w-[14rem] mx-auto my-2 p-1 text-c-error text-sm select-none opacity-0">
Error: {error()}
</p>

View File

@ -0,0 +1,3 @@
<div id="person-search-result">
<p>Create a new person here:</p>
</div>

View File

@ -0,0 +1,49 @@
<div id="person-search-result" x-show="user_dni.length >= 8">
<div class="relative max-w-[14rem] mx-auto my-6">
<label class="absolute -top-2 left-2 text-xs bg-c-surface px-1 select-none">
Apellido Paterno
</label>
<span class="bg-c-background text-c-on-background border-c-outline
border-2 rounded px-2 py-1 w-full inline-block font-mono
disabled:opacity-50 disabled:cursor-not-allowed">
AAAAAAA
</span>
<button type="button" class="absolute top-1 right-1 rounded hover:bg-c-surface-variant"
onclick={copyToClipboard}>
<i class="ph ph-copy inline-block w-6 text-2xl"></i>
</button>
</div>
<div class="relative max-w-[14rem] mx-auto my-6">
<label class="absolute -top-2 left-2 text-xs bg-c-surface px-1 select-none">
Apellido Materno
</label>
<span class="bg-c-background text-c-on-background border-c-outline
border-2 rounded px-2 py-1 w-full inline-block font-mono
disabled:opacity-50 disabled:cursor-not-allowed">
BBBBBB
</span>
<button type="button" class="absolute top-1 right-1 rounded hover:bg-c-surface-variant"
onclick={copyToClipboard}>
<i class="ph ph-copy inline-block w-6 text-2xl"></i>
</button>
</div>
<div class="relative max-w-[14rem] mx-auto my-6">
<label class="absolute -top-2 left-2 text-xs bg-c-surface px-1 select-none">
Nombres
</label>
<span class="bg-c-background text-c-on-background border-c-outline
border-2 rounded px-2 py-1 w-full inline-block font-mono
disabled:opacity-50 disabled:cursor-not-allowed">
CCCCCCCC
</span>
<button type="button" class="absolute top-1 right-1 rounded hover:bg-c-surface-variant"
onclick={copyToClipboard}>
<i class="ph ph-copy inline-block w-6 text-2xl"></i>
</button>
</div>
</div>

View File

@ -1,3 +0,0 @@
<div>
Sample response :D (carita feliz)
</div>

View File

@ -1,7 +1,7 @@
<!doctype html>
<html lang="es" xmlns:th="http://www.thymeleaf.org">
<head th:replace="fragments/head"></head>
<head th:replace="~{fragments/head :: head}"></head>
<body>