Skip to content
Open
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
16 changes: 16 additions & 0 deletions src/main/java/com/ironhack/users_micro/config/AppConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.ironhack.users_micro.config;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class AppConfig {

@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,7 @@ public List<User> getAllUsers() {

@GetMapping("/{id}")
public ResponseEntity<?> getUserById(@PathVariable long id) {
try {
User foundUser = userService.getUserById(id);
return new ResponseEntity<>(foundUser, HttpStatus.FOUND);
} catch (UserNotFoundException e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
}
return userService.getUserById(id);
}

@PostMapping
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/ironhack/users_micro/dto/AccountResponseDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.ironhack.users_micro.dto;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class AccountResponseDto {

private Long id;
private String isbn;
private BigDecimal balance;
}
21 changes: 21 additions & 0 deletions src/main/java/com/ironhack/users_micro/dto/UserResponseDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.ironhack.users_micro.dto;

import com.ironhack.users_micro.model.User;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserResponseDto {
private Long id;
private String username;
private AccountResponseDto account;

public UserResponseDto(User user, AccountResponseDto account) {
setId(user.getId());
setUsername(user.getUsername());
setAccount(account);
}
}
24 changes: 17 additions & 7 deletions src/main/java/com/ironhack/users_micro/service/UserService.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.ironhack.users_micro.service;

import com.ironhack.users_micro.dto.AccountResponseDto;
import com.ironhack.users_micro.dto.UserResponseDto;
import com.ironhack.users_micro.exception.UserNotFoundException;
import com.ironhack.users_micro.model.User;
import com.ironhack.users_micro.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Optional;
Expand All @@ -14,17 +18,23 @@
public class UserService {
private final UserRepository userRepository;

@Autowired
private RestTemplate restTemplate;

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

public User getUserById(long id){
public ResponseEntity<?> getUserById(long id) {
Optional<User> optionalUser = userRepository.findById(id);

if(optionalUser.isPresent()){
return optionalUser.get();
}else{
throw new UserNotFoundException("The user was not found");
if (optionalUser.isPresent()) {
AccountResponseDto account = restTemplate.getForObject("http://accounts-micro/api/account/" +
optionalUser.get().getAccountID(), AccountResponseDto.class);
UserResponseDto userResponseDto = new UserResponseDto(optionalUser.get(), account);
return new ResponseEntity<>(userResponseDto, HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}

Expand All @@ -44,9 +54,9 @@ public User patchAccountId(Long userId, Long accountId) {

foundUser.setAccountID(accountId);
userRepository.save(foundUser);
return foundUser;
return foundUser;

}else{
} else {
throw new UserNotFoundException("Couldn't find the user");
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
spring.application.name=users-micro
spring.datasource.url=jdbc:mysql://localhost:3306/users_micro
spring.datasource.username=root
spring.datasource.password=admin
spring.datasource.password=sasa
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.hibernate.ddl-auto=update
Expand Down