feat: add profiles page, dto
This commit is contained in:
parent
9fccc6e1de
commit
be986d1467
@ -9,9 +9,9 @@
|
|||||||
<div>
|
<div>
|
||||||
<form method="post" action="/profile">
|
<form method="post" action="/profile">
|
||||||
<c:if test="${requestScope.profile.id != null}">
|
<c:if test="${requestScope.profile.id != null}">
|
||||||
<input type = "hidden" name = "_method" value = "put"/>
|
<input type="hidden" name="_method" value="put"/>
|
||||||
</c:if>
|
</c:if>
|
||||||
<input type = "hidden" name="id" value="${requestScope.profile.id}">
|
<input type="hidden" name="id" value="${requestScope.profile.id}">
|
||||||
<table>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
<td><h3>${requestScope.wordBundle.getWord("email")}</h3></td>
|
<td><h3>${requestScope.wordBundle.getWord("email")}</h3></td>
|
||||||
@ -25,6 +25,14 @@
|
|||||||
<td><h3>${requestScope.wordBundle.getWord("surname")}</h3></td>
|
<td><h3>${requestScope.wordBundle.getWord("surname")}</h3></td>
|
||||||
<td><input type="text" name="surname" value="${requestScope.profile.surname}"></td>
|
<td><input type="text" name="surname" value="${requestScope.profile.surname}"></td>
|
||||||
</tr>
|
</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>
|
<tr>
|
||||||
<td><h3>${requestScope.wordBundle.getWord("about")}</h3></td>
|
<td><h3>${requestScope.wordBundle.getWord("about")}</h3></td>
|
||||||
<td><input type="text" name="about" value="${requestScope.profile.about}"></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><h3>${requestScope.wordBundle.getWord("gender")}</h3></td>
|
||||||
<td>
|
<td>
|
||||||
<select name="gender">
|
<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}">
|
<c:forEach var="gender" items="${applicationScope.genders}">
|
||||||
<option value="${gender}">${requestScope.wordBundle.getWord(gender)}</option>
|
<option value="${gender}">${requestScope.wordBundle.getWord(gender)}</option>
|
||||||
</c:forEach>
|
</c:forEach>
|
||||||
@ -45,8 +55,8 @@
|
|||||||
</form>
|
</form>
|
||||||
<c:if test="${requestScope.profile.id != null}">
|
<c:if test="${requestScope.profile.id != null}">
|
||||||
<form method="post" action="/profile">
|
<form method="post" action="/profile">
|
||||||
<input type = "hidden" name = "_method" value = "delete"/>
|
<input type="hidden" name="_method" value="delete"/>
|
||||||
<input type = "hidden" name="id" value="${requestScope.profile.id}">
|
<input type="hidden" name="id" value="${requestScope.profile.id}">
|
||||||
<button type="submit">${requestScope.wordBundle.getWord("delete")}</button>
|
<button type="submit">${requestScope.wordBundle.getWord("delete")}</button>
|
||||||
</form>
|
</form>
|
||||||
</c:if>
|
</c:if>
|
||||||
|
|||||||
37
resources/WEB-INF/jsp/profiles.jsp
Normal file
37
resources/WEB-INF/jsp/profiles.jsp
Normal 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>
|
||||||
@ -1,5 +1,7 @@
|
|||||||
about=About
|
about=About
|
||||||
|
age=Age
|
||||||
all-rights-reserved=All rights reserved
|
all-rights-reserved=All rights reserved
|
||||||
|
birth-date=Birth date
|
||||||
delete=Delete
|
delete=Delete
|
||||||
email=Email
|
email=Email
|
||||||
female=Female
|
female=Female
|
||||||
|
|||||||
@ -1,5 +1,7 @@
|
|||||||
about=About
|
about=About
|
||||||
|
age=Age
|
||||||
all-rights-reserved=All rights reserved
|
all-rights-reserved=All rights reserved
|
||||||
|
birth-date=Birth date
|
||||||
delete=Delete
|
delete=Delete
|
||||||
email=Email
|
email=Email
|
||||||
female=Female
|
female=Female
|
||||||
|
|||||||
@ -1,5 +1,7 @@
|
|||||||
about=\u041E \u0441\u0435\u0431\u0435
|
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
|
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
|
delete=\u0423\u0434\u0430\u043B\u0438\u0442\u044C
|
||||||
email=\u041F\u043E\u0447\u0442\u0430
|
email=\u041F\u043E\u0447\u0442\u0430
|
||||||
female=\u0416\u0435\u043D\u0449\u0438\u043D\u0430
|
female=\u0416\u0435\u043D\u0449\u0438\u043D\u0430
|
||||||
|
|||||||
@ -5,11 +5,13 @@ import jakarta.servlet.annotation.WebServlet;
|
|||||||
import jakarta.servlet.http.HttpServlet;
|
import jakarta.servlet.http.HttpServlet;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import ru.charm.back.dto.ProfileGetDto;
|
||||||
import ru.charm.back.model.Gender;
|
import ru.charm.back.model.Gender;
|
||||||
import ru.charm.back.model.Profile;
|
import ru.charm.back.model.Profile;
|
||||||
import ru.charm.back.service.ProfileService;
|
import ru.charm.back.service.ProfileService;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.time.LocalDate;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@WebServlet("/profile")
|
@WebServlet("/profile")
|
||||||
@ -21,11 +23,14 @@ public class ProfileController extends HttpServlet {
|
|||||||
String sId = req.getParameter("id");
|
String sId = req.getParameter("id");
|
||||||
String forwardUri = "/notFound";
|
String forwardUri = "/notFound";
|
||||||
if (sId != null) {
|
if (sId != null) {
|
||||||
Optional<Profile> profile = service.findById(Long.parseLong(sId));
|
Optional<ProfileGetDto> optProfileGetDto = service.findById(Long.parseLong(sId));
|
||||||
if (profile.isPresent()) {
|
if (optProfileGetDto.isPresent()) {
|
||||||
req.setAttribute("profile", profile.get());
|
req.setAttribute("profile", optProfileGetDto.get());
|
||||||
forwardUri = "/WEB-INF/jsp/profile.jsp";
|
forwardUri = "/WEB-INF/jsp/profile.jsp";
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
req.setAttribute("profiles", service.findAll());
|
||||||
|
forwardUri = "/WEB-INF/jsp/profiles.jsp";
|
||||||
}
|
}
|
||||||
req.getRequestDispatcher(forwardUri).forward(req, resp);
|
req.getRequestDispatcher(forwardUri).forward(req, resp);
|
||||||
}
|
}
|
||||||
@ -67,6 +72,7 @@ public class ProfileController extends HttpServlet {
|
|||||||
profile.setSurname(req.getParameter("surname"));
|
profile.setSurname(req.getParameter("surname"));
|
||||||
profile.setAbout(req.getParameter("about"));
|
profile.setAbout(req.getParameter("about"));
|
||||||
profile.setGender(Gender.valueOf(req.getParameter("gender")));
|
profile.setGender(Gender.valueOf(req.getParameter("gender")));
|
||||||
|
profile.setBirthday(LocalDate.parse(req.getParameter("birthDate")));
|
||||||
return profile;
|
return profile;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,7 +5,7 @@ import jakarta.servlet.annotation.WebServlet;
|
|||||||
import jakarta.servlet.http.HttpServlet;
|
import jakarta.servlet.http.HttpServlet;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import ru.charm.back.model.Profile;
|
import ru.charm.back.dto.ProfileGetDto;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
@ -13,7 +13,7 @@ import java.io.IOException;
|
|||||||
public class RegistrationController extends HttpServlet {
|
public class RegistrationController extends HttpServlet {
|
||||||
@Override
|
@Override
|
||||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
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);
|
req.getRequestDispatcher("/WEB-INF/jsp/profile.jsp").forward(req, resp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,6 +3,7 @@ package ru.charm.back.dao;
|
|||||||
import ru.charm.back.model.Gender;
|
import ru.charm.back.model.Gender;
|
||||||
import ru.charm.back.model.Profile;
|
import ru.charm.back.model.Profile;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
@ -24,6 +25,7 @@ public class ProfileDao {
|
|||||||
profile.setSurname("Ivanov");
|
profile.setSurname("Ivanov");
|
||||||
profile.setAbout("I am QA");
|
profile.setAbout("I am QA");
|
||||||
profile.setGender(Gender.MALE);
|
profile.setGender(Gender.MALE);
|
||||||
|
profile.setBirthday(LocalDate.parse("2000-01-01"));
|
||||||
this.storage.put(1L, profile);
|
this.storage.put(1L, profile);
|
||||||
Profile profile1 = new Profile();
|
Profile profile1 = new Profile();
|
||||||
profile1.setId(2L);
|
profile1.setId(2L);
|
||||||
@ -31,6 +33,7 @@ public class ProfileDao {
|
|||||||
profile1.setName("Elena");
|
profile1.setName("Elena");
|
||||||
profile1.setSurname("Sidorova");
|
profile1.setSurname("Sidorova");
|
||||||
profile1.setAbout("I am Java Dev");
|
profile1.setAbout("I am Java Dev");
|
||||||
|
profile1.setBirthday(LocalDate.parse("2000-01-01"));
|
||||||
profile1.setGender(Gender.FEMALE);
|
profile1.setGender(Gender.FEMALE);
|
||||||
this.storage.put(2L, profile1);
|
this.storage.put(2L, profile1);
|
||||||
this.idStorage = new AtomicLong(2L);
|
this.idStorage = new AtomicLong(2L);
|
||||||
|
|||||||
91
src/ru/charm/back/dto/ProfileGetDto.java
Normal file
91
src/ru/charm/back/dto/ProfileGetDto.java
Normal 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 + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
6
src/ru/charm/back/mapper/Mapper.java
Normal file
6
src/ru/charm/back/mapper/Mapper.java
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package ru.charm.back.mapper;
|
||||||
|
|
||||||
|
public interface Mapper<From, To> {
|
||||||
|
|
||||||
|
To map(From from);
|
||||||
|
}
|
||||||
32
src/ru/charm/back/mapper/ProfileGetDtoMapper.java
Normal file
32
src/ru/charm/back/mapper/ProfileGetDtoMapper.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,5 +1,7 @@
|
|||||||
package ru.charm.back.model;
|
package ru.charm.back.model;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
public class Profile {
|
public class Profile {
|
||||||
private Long id;
|
private Long id;
|
||||||
private String email;
|
private String email;
|
||||||
@ -7,6 +9,15 @@ public class Profile {
|
|||||||
private String surname;
|
private String surname;
|
||||||
private String about;
|
private String about;
|
||||||
private Gender gender;
|
private Gender gender;
|
||||||
|
private LocalDate birthday;
|
||||||
|
|
||||||
|
public LocalDate getBirthday() {
|
||||||
|
return birthday;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBirthday(LocalDate birthday) {
|
||||||
|
this.birthday = birthday;
|
||||||
|
}
|
||||||
|
|
||||||
public Long getId() {
|
public Long getId() {
|
||||||
return id;
|
return id;
|
||||||
|
|||||||
@ -1,6 +1,8 @@
|
|||||||
package ru.charm.back.service;
|
package ru.charm.back.service;
|
||||||
|
|
||||||
import ru.charm.back.dao.ProfileDao;
|
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 ru.charm.back.model.Profile;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -9,6 +11,7 @@ import java.util.Optional;
|
|||||||
public class ProfileService {
|
public class ProfileService {
|
||||||
private static final ProfileService INSTANCE = new ProfileService();
|
private static final ProfileService INSTANCE = new ProfileService();
|
||||||
private final ProfileDao dao = ProfileDao.getInstance();
|
private final ProfileDao dao = ProfileDao.getInstance();
|
||||||
|
private final ProfileGetDtoMapper profileGetDtoMapper = ProfileGetDtoMapper.getInstance();
|
||||||
|
|
||||||
|
|
||||||
public static ProfileService getInstance() {
|
public static ProfileService getInstance() {
|
||||||
@ -19,12 +22,12 @@ public class ProfileService {
|
|||||||
return dao.save(profile);
|
return dao.save(profile);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<Profile> findById(Long id) {
|
public Optional<ProfileGetDto> findById(Long id) {
|
||||||
if (id == null) {
|
if (id == null) {
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
return dao.findById(id);
|
return dao.findById(id).map(profileGetDtoMapper::map);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Boolean delete(Long id) {
|
public Boolean delete(Long id) {
|
||||||
@ -39,7 +42,7 @@ public class ProfileService {
|
|||||||
dao.update(profile);
|
dao.update(profile);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Profile> findAll() {
|
public List<ProfileGetDto> findAll() {
|
||||||
return dao.findAll();
|
return dao.findAll().stream().map(profileGetDtoMapper::map).toList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user