forked from vilelaricardo/mandacarubroker
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
246 additions
and
0 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
src/main/java/com/mandacarubroker/controller/UsersWalletController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.mandacarubroker.controller; | ||
|
||
import com.mandacarubroker.domain.users.Users; | ||
import com.mandacarubroker.domain.userswallet.GetAllDataTransferObject; | ||
import com.mandacarubroker.domain.userswallet.GetResponseDataTransferObject; | ||
import com.mandacarubroker.repository.UsersRepository; | ||
import com.mandacarubroker.service.UsersWalletService; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
import java.util.List; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
|
||
@SuppressWarnings("checkstyle:MissingJavadocType") | ||
@RestController | ||
@RequestMapping("wallet") | ||
public class UsersWalletController { | ||
private final UsersWalletService service; | ||
private final UsersRepository usersRepository; | ||
|
||
public UsersWalletController( | ||
UsersWalletService usersWalletService, | ||
UsersRepository usersRepository | ||
) { | ||
this.service = usersWalletService; | ||
this.usersRepository = usersRepository; | ||
} | ||
|
||
@Operation(summary = "Get all assets in the wallet", method = "GET") | ||
@ApiResponses(value = { | ||
@ApiResponse(responseCode = "200", description = "success"), | ||
}) | ||
@GetMapping("/{userId}") | ||
public ResponseEntity<GetResponseDataTransferObject> getAll(@PathVariable String userId) { | ||
Users findUser = this.usersRepository.findUsersById(userId); | ||
|
||
if (findUser == null) { | ||
return ResponseEntity | ||
.badRequest() | ||
.body(new GetResponseDataTransferObject( | ||
false, | ||
"User not found", | ||
null | ||
)); | ||
} | ||
|
||
List<GetAllDataTransferObject> response = service.getAll(userId); | ||
if (response.isEmpty()) { | ||
return ResponseEntity | ||
.ok() | ||
.body(new GetResponseDataTransferObject( | ||
false, | ||
"not found any register", | ||
null | ||
)); | ||
} | ||
|
||
return ResponseEntity | ||
.ok() | ||
.body(new GetResponseDataTransferObject( | ||
true, | ||
null, | ||
response | ||
)); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/mandacarubroker/domain/userswallet/GetAllDataTransferObject.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.mandacarubroker.domain.userswallet; | ||
|
||
public record GetAllDataTransferObject ( | ||
String id, | ||
String type_investment, | ||
String userId, | ||
String symbol, | ||
Integer amount, | ||
Double price | ||
){} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/mandacarubroker/domain/userswallet/GetResponseDataTransferObject.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.mandacarubroker.domain.userswallet; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Data encapsulation to response. | ||
* | ||
* @param success If response were successful | ||
* @param message The response message | ||
* @param data The response data | ||
* | ||
* */ | ||
public record GetResponseDataTransferObject( | ||
Boolean success, | ||
String message, | ||
List<GetAllDataTransferObject> data | ||
) {} |
26 changes: 26 additions & 0 deletions
26
src/main/java/com/mandacarubroker/domain/userswallet/RegisterDataTransferObject.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.mandacarubroker.domain.userswallet; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.NotNull; | ||
import jakarta.validation.constraints.Pattern; | ||
|
||
/** | ||
* | ||
* | ||
**/ | ||
public record RegisterDataTransferObject( | ||
@NotBlank(message = "The user ID cannot be blank") | ||
String userId, | ||
@NotBlank(message = "The type investment ID cannot be blank") | ||
String typeInvestmentId, | ||
@Pattern( | ||
regexp = "[A-Za-z]{4}\\d", | ||
message = "Symbol must be 4 letters followed by 1 number" | ||
) | ||
@NotBlank(message = "The symbol cannot be blank") | ||
String symbol, | ||
@NotNull(message = "The amount cannot be null") | ||
Integer amount, | ||
@NotNull(message = "The price cannot be null") | ||
Double price | ||
) {} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/mandacarubroker/domain/userswallet/ResponseDataTransferObject.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.mandacarubroker.domain.userswallet; | ||
|
||
/** | ||
* Data encapsulation to response. | ||
* | ||
* @param success If response were successful | ||
* @param message The response message | ||
* @param data The response data | ||
* | ||
* */ | ||
public record ResponseDataTransferObject( | ||
Boolean success, | ||
String message, | ||
Object data | ||
) {} |
37 changes: 37 additions & 0 deletions
37
src/main/java/com/mandacarubroker/domain/userswallet/UsersWallet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.mandacarubroker.domain.userswallet; | ||
|
||
import jakarta.persistence.*; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.NoArgsConstructor; | ||
|
||
/** | ||
* The Users Wallet class. | ||
* */ | ||
@Table(name = "users_wallet") | ||
@Entity(name = "users_wallet") | ||
@Data | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@EqualsAndHashCode(of = "id") | ||
public class UsersWallet { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.UUID) | ||
private String id; | ||
@Column(name = "user_id") | ||
private String userId; | ||
@Column(name = "type_investment_id") | ||
private String typeInvestmentId; | ||
private String symbol; | ||
private Integer amount; | ||
private Double price; | ||
|
||
public UsersWallet(RegisterDataTransferObject data) { | ||
this.userId = data.userId(); | ||
this.typeInvestmentId = data.typeInvestmentId(); | ||
this.symbol = data.symbol(); | ||
this.amount = data.amount(); | ||
this.price = data.price(); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/com/mandacarubroker/repository/UsersWalletRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.mandacarubroker.repository; | ||
|
||
import com.mandacarubroker.domain.userswallet.GetAllDataTransferObject; | ||
import com.mandacarubroker.domain.userswallet.UsersWallet; | ||
import java.util.List; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface UsersWalletRepository extends JpaRepository<UsersWallet, String> { | ||
@Query("SELECT new com.mandacarubroker.domain.userswallet.GetAllDataTransferObject(uw.id, ti.name, uw.userId, uw.symbol, uw.amount, uw.price) " + | ||
"FROM types_investments ti " + | ||
"JOIN users_wallet uw " + | ||
"ON ti.id = uw.typeInvestmentId") | ||
List<GetAllDataTransferObject> getAllByUserId(String userId); | ||
|
||
@Query("SELECT new com.mandacarubroker.domain.userswallet.GetAllDataTransferObject(uw.id, ti.name, uw.userId, uw.symbol, uw.amount, uw.price) " + | ||
"FROM types_investments ti " + | ||
"JOIN users_wallet uw " + | ||
"ON ti.id = uw.typeInvestmentId") | ||
UsersWallet getBySymbol(String symbol); | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/com/mandacarubroker/service/UsersWalletService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.mandacarubroker.service; | ||
|
||
import com.mandacarubroker.domain.userswallet.GetAllDataTransferObject; | ||
import com.mandacarubroker.domain.userswallet.RegisterDataTransferObject; | ||
import com.mandacarubroker.domain.userswallet.UsersWallet; | ||
import com.mandacarubroker.repository.UsersWalletRepository; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class UsersWalletService { | ||
private final UsersWalletRepository repository; | ||
|
||
public UsersWalletService( | ||
UsersWalletRepository usersWalletRepository | ||
) { | ||
this.repository = usersWalletRepository; | ||
} | ||
|
||
public List<GetAllDataTransferObject> getAll(String userId) { | ||
return repository.getAllByUserId(userId); | ||
} | ||
|
||
public UsersWallet create(RegisterDataTransferObject data) { | ||
UsersWallet newRegister = new UsersWallet(data); | ||
|
||
return repository.save(newRegister); | ||
} | ||
|
||
public Optional<UsersWallet> update(String id, RegisterDataTransferObject data) { | ||
return repository.findById(id) | ||
.map(item -> { | ||
item.setUserId(data.userId()); | ||
item.setTypeInvestmentId(data.typeInvestmentId()); | ||
item.setSymbol(data.symbol()); | ||
item.setAmount(data.amount()); | ||
item.setPrice(data.price()); | ||
|
||
return repository.save(item); | ||
}); | ||
} | ||
|
||
public void delete(String id) { | ||
repository.deleteById(id); | ||
} | ||
} |