feat: add profiles page, dto

This commit is contained in:
Sergey Krylov 2026-02-17 04:33:33 +03:00
parent 9fccc6e1de
commit be986d1467
13 changed files with 219 additions and 14 deletions

View File

@ -25,6 +25,14 @@
<td><h3>${requestScope.wordBundle.getWord("surname")}</h3></td>
<td><input type="text" name="surname" value="${requestScope.profile.surname}"></td>
</tr>
<tr>
<td><h3>${requestScope.wordBundle.getWord("birth-date")}</h3></td>
<td><input type="date" name="birthDate" value="${requestScope.profile.birthDate}"></td>
</tr>
<tr>
<td><h3>${requestScope.wordBundle.getWord("age")}</h3></td>
<td><h3>${requestScope.profile.age}</h3></td>
</tr>
<tr>
<td><h3>${requestScope.wordBundle.getWord("about")}</h3></td>
<td><input type="text" name="about" value="${requestScope.profile.about}"></td>
@ -33,7 +41,9 @@
<td><h3>${requestScope.wordBundle.getWord("gender")}</h3></td>
<td>
<select name="gender">
<option value="${requestScope.profile.gender}" selected hidden>${requestScope.profile.gender}</option>
<option value="${requestScope.profile.gender}" selected hidden>
${requestScope.profile.gender}
</option>
<c:forEach var="gender" items="${applicationScope.genders}">
<option value="${gender}">${requestScope.wordBundle.getWord(gender)}</option>
</c:forEach>

View File

@ -0,0 +1,37 @@
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html lang="en">
<head>
<title>Charm Profiles</title>
<style>
table, td {
border: 1px solid;
border-collapse: collapse;
}
</style>
</head>
<body>
<%@ include file="header.jsp" %>
<div>
<table>
<tr>
<td><h3>id</h3></td>
<td><h3>${requestScope.wordBundle.getWord("email")}</h3></td>
<td><h3>${requestScope.wordBundle.getWord("name")}</h3></td>
<td><h3>${requestScope.wordBundle.getWord("surname")}</h3></td>
<td><h3>${requestScope.wordBundle.getWord("age")}</h3></td>
</tr>
<c:forEach var="profile" items="${requestScope.profiles}">
<tr>
<td><h3>${profile.id}</h3></td>
<td><h3>${profile.email}</h3></td>
<td><h3>${profile.name}</h3></td>
<td><h3>${profile.surname}</h3></td>
<td><h3>${profile.age}</h3></td>
</tr>
</c:forEach>
</table>
</div>
<%@ include file="footer.jsp" %>
</body>
</html>

View File

@ -1,5 +1,7 @@
about=About
age=Age
all-rights-reserved=All rights reserved
birth-date=Birth date
delete=Delete
email=Email
female=Female

View File

@ -1,5 +1,7 @@
about=About
age=Age
all-rights-reserved=All rights reserved
birth-date=Birth date
delete=Delete
email=Email
female=Female

View File

@ -1,5 +1,7 @@
about=\u041E \u0441\u0435\u0431\u0435
age=\u0412\u043E\u0437\u0440\u0430\u0441\u0442
all-rights-reserved=\u0412\u0441\u0435 \u043F\u0440\u0430\u0432\u0430 \u0437\u0430\u0449\u0438\u0449\u0435\u043D\u044B
birth-date=\u0414\u0430\u0442\u0430 \u0440\u043E\u0436\u0434\u0435\u043D\u0438\u044F
delete=\u0423\u0434\u0430\u043B\u0438\u0442\u044C
email=\u041F\u043E\u0447\u0442\u0430
female=\u0416\u0435\u043D\u0449\u0438\u043D\u0430

View File

@ -5,11 +5,13 @@ import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import ru.charm.back.dto.ProfileGetDto;
import ru.charm.back.model.Gender;
import ru.charm.back.model.Profile;
import ru.charm.back.service.ProfileService;
import java.io.IOException;
import java.time.LocalDate;
import java.util.Optional;
@WebServlet("/profile")
@ -21,11 +23,14 @@ public class ProfileController extends HttpServlet {
String sId = req.getParameter("id");
String forwardUri = "/notFound";
if (sId != null) {
Optional<Profile> profile = service.findById(Long.parseLong(sId));
if (profile.isPresent()) {
req.setAttribute("profile", profile.get());
Optional<ProfileGetDto> optProfileGetDto = service.findById(Long.parseLong(sId));
if (optProfileGetDto.isPresent()) {
req.setAttribute("profile", optProfileGetDto.get());
forwardUri = "/WEB-INF/jsp/profile.jsp";
}
} else {
req.setAttribute("profiles", service.findAll());
forwardUri = "/WEB-INF/jsp/profiles.jsp";
}
req.getRequestDispatcher(forwardUri).forward(req, resp);
}
@ -67,6 +72,7 @@ public class ProfileController extends HttpServlet {
profile.setSurname(req.getParameter("surname"));
profile.setAbout(req.getParameter("about"));
profile.setGender(Gender.valueOf(req.getParameter("gender")));
profile.setBirthday(LocalDate.parse(req.getParameter("birthDate")));
return profile;
}
}

View File

@ -5,7 +5,7 @@ import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import ru.charm.back.model.Profile;
import ru.charm.back.dto.ProfileGetDto;
import java.io.IOException;
@ -13,7 +13,7 @@ import java.io.IOException;
public class RegistrationController extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setAttribute("profile", new Profile());
req.setAttribute("profile", new ProfileGetDto());
req.getRequestDispatcher("/WEB-INF/jsp/profile.jsp").forward(req, resp);
}
}

View File

@ -3,6 +3,7 @@ package ru.charm.back.dao;
import ru.charm.back.model.Gender;
import ru.charm.back.model.Profile;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@ -24,6 +25,7 @@ public class ProfileDao {
profile.setSurname("Ivanov");
profile.setAbout("I am QA");
profile.setGender(Gender.MALE);
profile.setBirthday(LocalDate.parse("2000-01-01"));
this.storage.put(1L, profile);
Profile profile1 = new Profile();
profile1.setId(2L);
@ -31,6 +33,7 @@ public class ProfileDao {
profile1.setName("Elena");
profile1.setSurname("Sidorova");
profile1.setAbout("I am Java Dev");
profile1.setBirthday(LocalDate.parse("2000-01-01"));
profile1.setGender(Gender.FEMALE);
this.storage.put(2L, profile1);
this.idStorage = new AtomicLong(2L);

View File

@ -0,0 +1,91 @@
package ru.charm.back.dto;
import ru.charm.back.model.Gender;
import java.time.LocalDate;
public class ProfileGetDto {
private Long id;
private String email;
private String name;
private String surname;
private String about;
private Gender gender;
private LocalDate birthDate;
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public LocalDate getBirthDate() {
return birthDate;
}
public void setBirthDate(LocalDate birthDate) {
this.birthDate = birthDate;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public String getAbout() {
return about;
}
public void setAbout(String about) {
this.about = about;
}
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Profile{" +
"id=" + id +
", email='" + email + '\'' +
", name='" + name + '\'' +
", surname='" + surname + '\'' +
", about='" + about + '\'' +
'}';
}
}

View File

@ -0,0 +1,6 @@
package ru.charm.back.mapper;
public interface Mapper<From, To> {
To map(From from);
}

View File

@ -0,0 +1,32 @@
package ru.charm.back.mapper;
import ru.charm.back.dto.ProfileGetDto;
import ru.charm.back.model.Profile;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public class ProfileGetDtoMapper implements Mapper<Profile, ProfileGetDto> {
private static final ProfileGetDtoMapper INSTANCE = new ProfileGetDtoMapper();
public static ProfileGetDtoMapper getInstance() {
return INSTANCE;
}
@Override
public ProfileGetDto map(Profile profile) {
ProfileGetDto dto = new ProfileGetDto();
dto.setId(profile.getId());
dto.setEmail(profile.getEmail());
dto.setName(profile.getName());
dto.setSurname(profile.getSurname());
dto.setBirthDate(profile.getBirthday());
dto.setAge(Math.toIntExact(ChronoUnit.YEARS.between(profile.getBirthday(), LocalDate.now())));
dto.setGender(profile.getGender());
dto.setAbout(profile.getAbout());
return dto;
}
}

View File

@ -1,5 +1,7 @@
package ru.charm.back.model;
import java.time.LocalDate;
public class Profile {
private Long id;
private String email;
@ -7,6 +9,15 @@ public class Profile {
private String surname;
private String about;
private Gender gender;
private LocalDate birthday;
public LocalDate getBirthday() {
return birthday;
}
public void setBirthday(LocalDate birthday) {
this.birthday = birthday;
}
public Long getId() {
return id;

View File

@ -1,6 +1,8 @@
package ru.charm.back.service;
import ru.charm.back.dao.ProfileDao;
import ru.charm.back.dto.ProfileGetDto;
import ru.charm.back.mapper.ProfileGetDtoMapper;
import ru.charm.back.model.Profile;
import java.util.List;
@ -9,6 +11,7 @@ import java.util.Optional;
public class ProfileService {
private static final ProfileService INSTANCE = new ProfileService();
private final ProfileDao dao = ProfileDao.getInstance();
private final ProfileGetDtoMapper profileGetDtoMapper = ProfileGetDtoMapper.getInstance();
public static ProfileService getInstance() {
@ -19,12 +22,12 @@ public class ProfileService {
return dao.save(profile);
}
public Optional<Profile> findById(Long id) {
public Optional<ProfileGetDto> findById(Long id) {
if (id == null) {
return Optional.empty();
}
return dao.findById(id);
return dao.findById(id).map(profileGetDtoMapper::map);
}
public Boolean delete(Long id) {
@ -39,7 +42,7 @@ public class ProfileService {
dao.update(profile);
}
public List<Profile> findAll() {
return dao.findAll();
public List<ProfileGetDto> findAll() {
return dao.findAll().stream().map(profileGetDtoMapper::map).toList();
}
}