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
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.ironhack.users_micro.controller;

import com.ironhack.users_micro.dto.UserAccountDTO;
import com.ironhack.users_micro.dto.UserPatchAccountDTO;
import com.ironhack.users_micro.exception.UserNotFoundException;
import com.ironhack.users_micro.model.User;
import com.ironhack.users_micro.service.AccountService;
import com.ironhack.users_micro.service.UserService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -14,24 +16,43 @@
@RequestMapping("/api/user")
public class UserController {
private final UserService userService;
private final AccountService accountService;

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

@GetMapping
public List<User> getAllUsers() {
return userService.getAllUsers();
}

@GetMapping("/{id}")
/*@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);
}
}*/

@GetMapping("/{id}")
public ResponseEntity<?> getUserById(@PathVariable long id) {
try {
User foundUser = userService.getUserById(id);
UserAccountDTO account = accountService.getAccountByOwnerId(foundUser.getAccountID());
UserAccountDTO response = new UserAccountDTO(
account.getId(),
account.getOwnerId(),
account.getIsbn(),
account.getBalance()
);
return new ResponseEntity<>(response, HttpStatus.OK);
} catch (UserNotFoundException e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
}
}

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

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

import java.math.BigDecimal;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserAccountDTO {
private Long id;
private Long ownerId;
private String isbn;
private BigDecimal balance;

}
19 changes: 19 additions & 0 deletions src/main/java/com/ironhack/users_micro/service/AccountService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.ironhack.users_micro.service;

import com.ironhack.users_micro.dto.UserAccountDTO;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class AccountService {
private final RestTemplate restTemplate;

public AccountService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}

public UserAccountDTO getAccountByOwnerId(Long ownerId) {
String url = "http://localhost:8081/api/account/" + ownerId;
return restTemplate.getForObject(url, UserAccountDTO.class);
}
}