158 lines
7.4 KiB
C#
158 lines
7.4 KiB
C#
using System.ComponentModel;
|
|
using BrokerApiCore;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Swashbuckle.AspNetCore.Annotations;
|
|
|
|
namespace BrokerApiServer;
|
|
|
|
[Route("api/v1/planet/user")]
|
|
[ApiController, SwaggerTag("**플래닛 유저의 로그인 및 교환 처리**<br> - /api/v1/planet/auth를 통해 accessToken을 발급받은 후 사용 <br> - Bearer 형식 Hearder로 인증")]
|
|
public class PlanetUserController : PlanetAuthControllerBase
|
|
{
|
|
private readonly PlanetItemExchangeService m_exchange_service;
|
|
private readonly UserAuthService m_user_auth_service;
|
|
|
|
public PlanetUserController(
|
|
PlanetItemExchangeService exchangeService, UserAuthService userAuthService)
|
|
{
|
|
m_exchange_service = exchangeService;
|
|
m_user_auth_service = userAuthService;
|
|
}
|
|
|
|
[HttpPost, Route("login"), RequirePlanetAuth]
|
|
[Produces("application/json")]
|
|
[ProducesResponseType(typeof(LoginResponse), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(typeof(ApiErrorResponse), StatusCodes.Status400BadRequest)]
|
|
[SwaggerOperation(Summary = "웹 포털 런처 토큰으로 칼리버스 유저 인증 처리 [Bearer 인증 필요]",
|
|
Description = "플래닛의 클라이언트 구동 시 항상 유저 인증 후 실행해야 함")]
|
|
public async Task<IActionResult> login([FromBody] LoginRequest request)
|
|
{
|
|
Guard.Against.isNull(request, ServerErrorCode.InvalidRequest, ()=>"Request is empty");
|
|
Guard.Against.isNullOrEmptyOrWhiteSpace(request.WebPortalToken, ServerErrorCode.InvalidRequest,
|
|
()=>"WebPortalToken does not exist");
|
|
|
|
var result = await m_user_auth_service.authByWebPortalToken(request.WebPortalToken, this.PlanetId);
|
|
Guard.Against.resultFail(result);
|
|
|
|
return Ok(new LoginResponse
|
|
{
|
|
UserGuid = m_user_auth_service.UserGuid, Nickname = m_user_auth_service.Nickname,
|
|
});
|
|
}
|
|
|
|
[HttpPost("currency"), RequirePlanetAuth]
|
|
[Produces("application/json")]
|
|
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(CurrencyBalanceResponse))]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest, Type = typeof(ApiErrorResponse))]
|
|
[SwaggerOperation(Summary = "칼리버스 측 재화 현재 수량 조회 [Bearer 인증 필요]")]
|
|
public async Task<IActionResult> getCurrencyBalance(CurrencyBalanceRequest request)
|
|
{
|
|
var sso_account_id = request.SsoAccountId ?? string.Empty;
|
|
var user_guid = request.UserGuid ?? string.Empty;
|
|
await m_exchange_service.validateAndGetUser(sso_account_id, user_guid, this.PlanetId);
|
|
var current_sapphire_amount = await m_exchange_service.getCurrencyAmount(request.CaliverseCurrencyType);
|
|
return Ok(new CurrencyBalanceResponse { CaliverseCurrencyType = request.CaliverseCurrencyType, Amount = current_sapphire_amount });
|
|
}
|
|
|
|
[HttpPost("exchange/order/list"), RequirePlanetAuth]
|
|
[Produces("application/json")]
|
|
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(PlanetItemExchangeOrderListResponse))]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest, Type = typeof(ApiErrorResponse))]
|
|
[SwaggerOperation(Summary = "교환 주문 목록 조회 [Bearer 인증 필요]")]
|
|
public async Task<IActionResult> getOrders(
|
|
[SwaggerRequestBody] PlanetItemExchangeOrderListRequest request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var planet_id = string.IsNullOrEmpty(request.PlanetId) ? this.PlanetId : request.PlanetId;
|
|
var user_guid = request.UserGuid;
|
|
var account_id = request.SsoAccountId;
|
|
await m_exchange_service.validateAndGetUser(account_id, user_guid, this.PlanetId);
|
|
user_guid = m_exchange_service.PlanetUser.UserGuid;
|
|
account_id = m_exchange_service.PlanetUser.AccountId;
|
|
|
|
ExchangeOrderStatus? order_status = request switch
|
|
{
|
|
{ Option: FindOption.Completed } => ExchangeOrderStatus.Completed,
|
|
{ Option: FindOption.Pending } => ExchangeOrderStatus.Pending,
|
|
_ => null // 전체 조회
|
|
};
|
|
var season_id = request.SeasonId;
|
|
var request_exchange_meta_id = request.ExchangeMetaId;
|
|
var page_index = request.PageIndex <= 0 ? 1 : request.PageIndex;
|
|
var page_size = request.PageSize <= 0 ? 20 : request.PageSize;
|
|
var (orders, total_count) =
|
|
await m_exchange_service.findOrderList(planet_id, request_exchange_meta_id, season_id, user_guid, order_status, page_index,
|
|
page_size, "desc", cancellationToken);
|
|
|
|
var order_dtos = orders.Select(x => new PlanetItemExchangeOrderDto
|
|
{
|
|
OrderId = x.OrderId,
|
|
OrderStatus = x.OrderStatus,
|
|
SeasonId = x.SeasonId,
|
|
ExchangeMetaId = x.ExchangeMetaId,
|
|
ExchangeMetaAmount = x.ExchangeMetaAmount,
|
|
AccountId = x.AccountId,
|
|
UserGuid = x.UserGuid,
|
|
PlanetId = x.PlanetId,
|
|
CaliverseItemType = x.CaliverseItemType,
|
|
CaliverseItemId = x.CaliverseItemId,
|
|
CaliverseItemDeltaAmount = x.CaliverseItemDeltaAmount,
|
|
PlanetItemType = x.PlanetItemType,
|
|
PlanetItemId = x.PlanetItemId,
|
|
PlanetItemDeltaAmount = x.PlanetItemDeltaAmount,
|
|
CreatedAt = x.CreatedAt,
|
|
CompletedAt = x.CompletedAt,
|
|
});
|
|
return Ok(new PlanetItemExchangeOrderListResponse { Orders = order_dtos, TotalCount = total_count });
|
|
}
|
|
|
|
[HttpPost("exchange/order/create"), RequirePlanetAuth]
|
|
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(PlanetItemExchangeResponse))]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest, Type = typeof(ApiErrorResponse)), Description("에러 발생 시 처리")]
|
|
[Produces("application/json")]
|
|
[SwaggerOperation(Summary = "교환 주문 체결 요청 [Bearer 인증 필요]")]
|
|
public async Task<IActionResult> createOrder(
|
|
PlanetItemExchangeRequest request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var sso_account_id = string.Empty;
|
|
var user_guid = request.UserGuid;
|
|
var season_id = request.SeasonId;
|
|
await m_exchange_service.validateAndGetUser(sso_account_id, user_guid, this.PlanetId);
|
|
// 메타버스 클라이언트에서 정상적으로
|
|
Guard.Against.isTrue(m_exchange_service.isUserLoggedIn(),
|
|
ServerErrorCode.MetaverseClientOnConnected,
|
|
()=>"메타버스에 접속 중인 상태. 메타버스에서 정상적으로 로그아웃 한 후에 다시 시도 바람.");
|
|
|
|
var (result, order) =
|
|
await m_exchange_service.createOrder(this.PlanetId, season_id, this.PlanetServerType, request.ExchangeMetaId,
|
|
request.ExchangeMetaAmount, cancellationToken);
|
|
Guard.Against.resultFail(result, ServerErrorCode.InternalServerError, ()=>result.ResultString);
|
|
|
|
var response = new PlanetItemExchangeResponse { ExchangeOrder = order.toDto(), };
|
|
return Ok(response);
|
|
}
|
|
|
|
[HttpPost("exchange/order/complete"), RequirePlanetAuth]
|
|
[Produces("application/json")]
|
|
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(PlanetItemExchangeResponse))]
|
|
[SwaggerResponse(StatusCodes.Status400BadRequest, "교환 주문 완료 실패<br>실패 사유를 확인하세요.",
|
|
typeof(ApiErrorResponse))]
|
|
[SwaggerOperation(Summary = "교환 주문 완료 요청 [Bearer 인증 필요]")]
|
|
public async Task<IActionResult> completeOrder(
|
|
PlanetItemExchangeCompleteRequest request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var sso_account_id = string.Empty;
|
|
await m_exchange_service.validateAndGetUser(sso_account_id, request.UserGuid, this.PlanetId);
|
|
// 메타버스 클라이언트에서 정상적으로
|
|
Guard.Against.isTrue(m_exchange_service.isUserLoggedIn(),
|
|
ServerErrorCode.MetaverseClientOnConnected,
|
|
()=>"메타버스에 접속 중인 상태. 메타버스에서 정상적으로 로그아웃 한 후에 다시 시도 바람.");
|
|
|
|
var order = await m_exchange_service.completeOrder(request.ExchangeOrderId, cancellationToken);
|
|
var response = new PlanetItemExchangeResponse { ExchangeOrder = order.toDto() };
|
|
return Ok(response);
|
|
}
|
|
}
|