Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package ua.com.javarush.gnew.contactm.DTOs;

import java.util.ArrayList;
import java.util.List;
import lombok.*;

@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class ContactDTO {
Expand All @@ -17,4 +17,10 @@ public class ContactDTO {
private List<EmailDTO> emails;
private List<PhoneDTO> phones;
private List<SocialNetworkDTO> networks;

public ContactDTO() {
this.emails = new ArrayList<>();
this.phones = new ArrayList<>();
this.networks = new ArrayList<>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import ua.com.javarush.gnew.contactm.DTOs.ContactDTO;
import ua.com.javarush.gnew.contactm.DTOs.EmailDTO;
import ua.com.javarush.gnew.contactm.DTOs.PhoneDTO;
import ua.com.javarush.gnew.contactm.DTOs.SocialNetworkDTO;
import ua.com.javarush.gnew.contactm.mapper.ContactMapper;
import ua.com.javarush.gnew.contactm.services.CloudinaryService;
import ua.com.javarush.gnew.contactm.services.ContactService;
Expand All @@ -23,7 +26,12 @@ public class ContactController {

@GetMapping("/edit/{id}")
public String edit(@PathVariable Long id, Model model) {

ContactDTO dto = contactMapper.toDto(contactService.findById(id));
if (dto.getEmails().isEmpty()) dto.getEmails().add(new EmailDTO());
if (dto.getPhones().isEmpty()) dto.getPhones().add(new PhoneDTO());
if (dto.getNetworks().isEmpty()) dto.getNetworks().add(new SocialNetworkDTO());

model.addAttribute("contact", dto);
return "contact/edit";
}
Expand Down Expand Up @@ -54,6 +62,41 @@ public String editContact(
return "redirect:/";
}

@GetMapping("/add")
public String add(Model model) {
ContactDTO dto = new ContactDTO();
dto.getEmails().add(new EmailDTO());
dto.getPhones().add(new PhoneDTO());
dto.getNetworks().add(new SocialNetworkDTO());
model.addAttribute("contact", dto);

return "contact/edit";
}

@PostMapping("/add")
public String saveContact(
@ModelAttribute ContactDTO contactDTO,
@RequestParam(value = "imageFile", required = false) MultipartFile imageFile,
RedirectAttributes redirectAttributes) {

try {
if (imageFile != null && !imageFile.isEmpty()) {
String imageUrl = imageService.upload(imageFile);
contactDTO.setImageUrl(imageUrl);
}

contactService.save(contactDTO);
redirectAttributes.addFlashAttribute("success", "Contact added successfully!");

} catch (IOException e) {
redirectAttributes.addFlashAttribute("error", "Failed to upload image: " + e.getMessage());
} catch (Exception e) {
redirectAttributes.addFlashAttribute("error", "Failed to add contact: " + e.getMessage());
}

return "redirect:/";
}

@PostMapping("/remove/{id}")
public String remove(@PathVariable Long id) {
contactService.delete(id);
Expand Down
13 changes: 11 additions & 2 deletions src/main/resources/templates/home.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.w3.org/1999/xhtml">

<head>
<meta charset="UTF-8">
Expand All @@ -14,7 +14,16 @@
<div th:if="${param.continue}">You have been logged in successfuly</div>
<div class="">
<!-- Display the project name -->
<div th:text="${tableName}"></div>
<div class="flex items-center justify-between mb-2">
<div th:text="${tableName}"></div>

<div sec:authorize="isAuthenticated()" style="transform: translateX(-150px);">
<a th:href="@{/contact/add}"
class="inline-flex items-center px-5 py-3 text-sm font-medium text-green-600 bg-green-50 border border-green-200 rounded-md hover:bg-green-100 transition-colors">
Add Contact
</a>
</div>
</div>

<!--/*@thymesVar id="contacts" type="java.util.List"*/-->
<div th:replace="~{components/table-component :: contactTable( ${contacts} ) }"></div>
Expand Down