Skip to content

Commit

Permalink
Merge pull request #1 from Hangar-Tech/feat-users
Browse files Browse the repository at this point in the history
Feat users
  • Loading branch information
MatheusVict authored Feb 21, 2024
2 parents 4f76f46 + ea36a40 commit da85960
Show file tree
Hide file tree
Showing 18 changed files with 494 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.institutosemprealerta.semprealerta.application.service;

import com.institutosemprealerta.semprealerta.infrastructure.entity.user.User;

public interface UserService {
void save(User user);
void update(int id, User user);
void delete(int id);
User findByRegistration(String registration);
User findByEmail(String email);
User findById(int id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.institutosemprealerta.semprealerta.application.service.impl;

import com.institutosemprealerta.semprealerta.application.service.UserService;
import com.institutosemprealerta.semprealerta.infrastructure.entity.user.User;
import com.institutosemprealerta.semprealerta.domain.ports.out.UserRepository;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

private final UserRepository userRepository;

public UserServiceImpl(UserRepository userRepository) {
this.userRepository = userRepository;
}

@Override
public void save(User user) {
this.userRepository.save(user);
}

@Override
public void update(int id, User user) {
this.userRepository.update(id, user);
}

@Override
public void delete(int id) {
this.userRepository.delete(id);
}

@Override
public User findByRegistration(String registration) {
return this.userRepository.findByRegistration(registration)
.orElseThrow(() -> new RuntimeException("User not found"));
}

@Override
public User findByEmail(String email) {
return this.userRepository.findByEmail(email)
.orElseThrow(() -> new RuntimeException("User not found"));
}

@Override
public User findById(int id) {
return this.userRepository.findById(id)
.orElseThrow(() -> new RuntimeException("User not found"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.institutosemprealerta.semprealerta.domain.model;


import com.fasterxml.jackson.annotation.JsonFormat;
import com.institutosemprealerta.semprealerta.infrastructure.entity.user.Address;
import com.institutosemprealerta.semprealerta.infrastructure.entity.user.Contact;
import com.institutosemprealerta.semprealerta.infrastructure.entity.user.User;
import com.institutosemprealerta.semprealerta.infrastructure.entity.user.UserRoles;
import com.institutosemprealerta.semprealerta.utils.DateManipulation;
import jakarta.validation.constraints.*;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public record UserDTO(
@NotBlank
String name,
@Email
String email,
@NotBlank
String password,
@NotBlank
String phone,
String gender,
@PastOrPresent
LocalDate birthDate,
@NotNull
UserRoles roles,
String street,
String number,
String city,
String zipCode

) {
public User toDomain() {
//LocalDate birth = DateManipulation.stringToLocalDate(birthDate);
Contact contact = new Contact(
email,
phone
);

Address address = new Address(
street,
number,
city,
zipCode
);
return new User(
name,
password,
gender,
birthDate,
roles,
contact,
address
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.institutosemprealerta.semprealerta.domain.ports.out;

import com.institutosemprealerta.semprealerta.infrastructure.entity.user.User;

import java.util.Optional;

public interface UserRepository {
void save(User user);
Optional<User> findById(int id);
void update(int id, User user);
void delete(int id);
Optional<User> findByRegistration(String registration);
Optional<User> findByEmail(String email);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.institutosemprealerta.semprealerta.domain.ports.out;

import com.institutosemprealerta.semprealerta.infrastructure.entity.user.Address;
import com.institutosemprealerta.semprealerta.infrastructure.entity.user.Contact;
import com.institutosemprealerta.semprealerta.infrastructure.entity.user.User;
import com.institutosemprealerta.semprealerta.infrastructure.entity.user.UserRoles;
import jakarta.persistence.Embedded;

import java.time.LocalDate;

public record UserResponse(
String registration,
String name,

String gender,
LocalDate birthDate,

UserRoles roles,

Contact contact,
Address address
) {

public static UserResponse toResponse(User user) {
return new UserResponse(
user.getRegistration(),
user.getName(),
user.getGender(),
user.getBirthDate(),
user.getRoles(),
user.getContact(),
user.getAddress()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.institutosemprealerta.semprealerta.infrastructure.adpters;

import com.institutosemprealerta.semprealerta.domain.ports.out.UserRepository;
import com.institutosemprealerta.semprealerta.infrastructure.entity.user.User;
import com.institutosemprealerta.semprealerta.infrastructure.repositories.JpaUserRepository;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
public class JpaUserRepositoryAdapter implements UserRepository {

private final JpaUserRepository userRepository;

public JpaUserRepositoryAdapter(JpaUserRepository jpaUserRepository) {
this.userRepository = jpaUserRepository;
}

@Override
public void save(User user) {
this.userRepository.save(user);
}

@Override
public Optional<User> findById(int id) {
return this.userRepository.findById(id);
}

@Override
public void update(int id, User user) {
User userToUpdate = this.userRepository.findById(id)
.orElseThrow(() -> new RuntimeException("User not found"));

user.setId(userToUpdate.getId());
user.setRegistration(userToUpdate.getRegistration());

this.userRepository.save(user);
}

@Override
public void delete(int id) {
User userToDelete = this.userRepository.findById(id)
.orElseThrow(() -> new RuntimeException("User not found"));

this.userRepository.delete(userToDelete);
}

@Override
public Optional<User> findByRegistration(String registration) {
return this.userRepository.findByRegistration(registration);
}

@Override
public Optional<User> findByEmail(String email) {
return this.userRepository.findByEmail(email);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.institutosemprealerta.semprealerta.infrastructure.controllers;

import com.institutosemprealerta.semprealerta.application.service.UserService;
import com.institutosemprealerta.semprealerta.domain.model.UserDTO;
import com.institutosemprealerta.semprealerta.domain.ports.out.UserResponse;
import com.institutosemprealerta.semprealerta.infrastructure.entity.user.User;
import jakarta.validation.Valid;
import org.apache.catalina.connector.Response;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("api/v1/user")
public class UserController {

private final UserService userService;

public UserController(UserService userService) {
this.userService = userService;
}

@PostMapping
public ResponseEntity<?> createUser(@Valid @RequestBody UserDTO user) {
userService.save(user.toDomain());
return ResponseEntity.status(Response.SC_CREATED).build();
}

@GetMapping("/{id}")
public ResponseEntity<UserResponse> findById(@PathVariable int id) {
User userFound = userService.findById(id);
return ResponseEntity.ok().body(UserResponse.toResponse(userFound));
}

@GetMapping("/registration/{reg}")
public ResponseEntity<UserResponse> findByRegistration(@PathVariable String reg) {
User userFound = userService.findByRegistration(reg);
return ResponseEntity.ok().body(UserResponse.toResponse(userFound));
}

@PutMapping("/{id}")
public ResponseEntity<?> updateUser(@PathVariable int id, @RequestBody UserDTO user) {
userService.update(id, user.toDomain());
return ResponseEntity.status(Response.SC_NO_CONTENT).build();
}

@DeleteMapping("/{id}")
public ResponseEntity<?> deleteUser(@PathVariable int id) {
userService.delete(id);
return ResponseEntity.status(Response.SC_NO_CONTENT).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.institutosemprealerta.semprealerta.infrastructure.entity.user;

import jakarta.persistence.Embeddable;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Embeddable
public class Address {
private String street;
private String number;
private String city;
private String zipCode;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.institutosemprealerta.semprealerta.infrastructure.entity.user;

import jakarta.persistence.Embeddable;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Embeddable
public class Contact {
private String email;
private String phone;
}
Loading

0 comments on commit da85960

Please sign in to comment.