Skip to content

Commit

Permalink
✨ feat: Create users wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
I-Lima committed Mar 21, 2024
1 parent 7f5b76a commit 0a13e76
Show file tree
Hide file tree
Showing 8 changed files with 246 additions and 0 deletions.
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
));
}
}
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
){}
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
) {}
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
) {}
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
) {}
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();
}
}
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 src/main/java/com/mandacarubroker/service/UsersWalletService.java
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);
}
}

0 comments on commit 0a13e76

Please sign in to comment.