-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/crud usuario #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
4bc256a
8f84ff2
65b692f
a39c850
87ff915
721b2a3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ? |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package com.orderflow.ecommerce.controllers; | ||
|
|
||
| import com.orderflow.ecommerce.dtos.UserRequestDTO; | ||
| import com.orderflow.ecommerce.services.UserService; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/users") | ||
| public class UserController { | ||
|
|
||
| @Autowired | ||
| private UserService userService; | ||
|
|
||
| @PostMapping | ||
| public ResponseEntity<UserRequestDTO> createUser(@RequestBody UserRequestDTO dto) { | ||
| UserRequestDTO savedUser = userService.create(dto); | ||
| return ResponseEntity.status(HttpStatus.CREATED).body(savedUser); | ||
| } | ||
|
|
||
| @GetMapping | ||
| public ResponseEntity<List<UserRequestDTO>> getAllUsers() { | ||
| List<UserRequestDTO> users = userService.findAll(); | ||
| return ResponseEntity.ok(users); | ||
| } | ||
|
|
||
| @GetMapping("/{id}") | ||
| public ResponseEntity<UserRequestDTO> getUserById(@PathVariable Long id) { | ||
| return ResponseEntity.ok(userService.findById(id)); | ||
| } | ||
|
|
||
| @PutMapping("/{id}") | ||
| public ResponseEntity<UserRequestDTO> updateUser(@PathVariable Long id, @RequestBody UserRequestDTO dto) { | ||
| return ResponseEntity.ok(userService.update(id, dto)); | ||
| } | ||
|
|
||
| @DeleteMapping("/{id}") | ||
| public ResponseEntity<Void> deleteUser(@PathVariable Long id) { | ||
| userService.delete(id); | ||
| return ResponseEntity.noContent().build(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| package com.orderflow.ecommerce.dtos; | ||
|
|
||
| public record UserRequestDTO(Long id, String name, String email, String password) { | ||
| } |
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package com.orderflow.ecommerce.entities; | ||
|
|
||
| import jakarta.persistence.*; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Data; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Entity | ||
| @Table(name = "tb_users") | ||
| @Data | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| public class User { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| private Long id; | ||
|
|
||
| private String name; | ||
|
|
||
| @Column(unique = true, nullable = false) | ||
| private String email; | ||
|
|
||
| @Column(nullable = false) | ||
| private String password; | ||
| } |
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why? |
This file was deleted.
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why? |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package com.orderflow.ecommerce.repositories; | ||
|
|
||
| import com.orderflow.ecommerce.entities.User; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.stereotype.Repository; | ||
| import java.util.Optional; | ||
|
|
||
| @Repository | ||
| public interface UserRepository extends JpaRepository<User, Long> { | ||
|
|
||
| Optional<User> findByEmail(String email); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package com.orderflow.ecommerce.services; | ||
|
|
||
| import com.orderflow.ecommerce.dtos.UserRequestDTO; | ||
| import com.orderflow.ecommerce.entities.User; | ||
| import com.orderflow.ecommerce.repositories.UserRepository; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| @Service | ||
| public class UserService { | ||
|
|
||
| @Autowired | ||
| private UserRepository userRepository; | ||
|
|
||
| public UserRequestDTO create(UserRequestDTO dto) { | ||
| User user = new User(); | ||
| user.setName(dto.name()); | ||
| user.setEmail(dto.email()); | ||
| user.setPassword(dto.password()); | ||
|
|
||
| user = userRepository.save(user); | ||
| // Retornamos com o ID que o banco gerou | ||
| return new UserRequestDTO(user.getId(), user.getName(), user.getEmail(), null); | ||
| } | ||
|
|
||
| public List<UserRequestDTO> findAll() { | ||
| return userRepository.findAll().stream().map(user -> | ||
| new UserRequestDTO(user.getId(), user.getName(), user.getEmail(), null) | ||
| ).collect(Collectors.toList()); | ||
| } | ||
|
|
||
| public UserRequestDTO findById(Long id) { | ||
| User user = userRepository.findById(id) | ||
| .orElseThrow(() -> new RuntimeException("Usuário não encontrado")); | ||
| return new UserRequestDTO(user.getId(), user.getName(), user.getEmail(), null); | ||
| } | ||
|
|
||
| public UserRequestDTO update(Long id, UserRequestDTO dto) { | ||
| User user = userRepository.findById(id) | ||
| .orElseThrow(() -> new RuntimeException("Usuário não encontrado")); | ||
|
|
||
| user.setName(dto.name()); | ||
| user.setEmail(dto.email()); | ||
| // Se quiser atualizar a senha também, descomente a linha abaixo: | ||
| // user.setPassword(dto.password()); | ||
|
|
||
| userRepository.save(user); | ||
| return new UserRequestDTO(user.getId(), user.getName(), user.getEmail(), null); | ||
| } | ||
|
|
||
| public void delete(Long id) { | ||
| userRepository.deleteById(id); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| spring.profiles.active=dev | ||
|
|
||
| spring.datasource.url=jdbc:postgresql://postgres:5432/orderflow | ||
| spring.datasource.username=orderflow | ||
| spring.datasource.password=orderflow123 | ||
| spring.datasource.driver-class-name=org.postgresql.Driver | ||
|
|
||
| spring.jpa.hibernate.ddl-auto=update | ||
| spring.jpa.show-sql=true | ||
| spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect | ||
|
|
||
| spring.rabbitmq.host=rabbitmq | ||
| spring.rabbitmq.port=5672 | ||
| spring.rabbitmq.username=orderflow | ||
| spring.rabbitmq.password=orderflow123 |
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Must not contain changes in this file |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?