public async Task <IActionResult> Get(CancellationToken cancellationToken = default) { var response = new GetUserResponse(); try { // get company and email data from jwt var company = _httpContext.HttpContext.User.Claims.First(c => c.Type == "company")?.Value; var email = _httpContext.HttpContext.User.Claims.First(c => c.Type == "username")?.Value; var filter = new GetUserFilter(company, email); var user = await _userAppService.GetUser(filter, cancellationToken); response.StatusCode = 200; response.Data = user; return(Ok(response)); } catch (Exception ex) { response.StatusCode = 500; response.Messages.Add(ResponseMessage.Create(ex, "")); return(StatusCode(500, response)); } }
public async Task <ActionResult <UpdateTemplateResponse> > Put([FromRoute] string clientID, [FromRoute] int id, [FromBody] UpdateTemplateRequest request) { UpdateTemplateResponse response = new UpdateTemplateResponse(); string responseMessage = $"UPDATE_{clientID}_TEMPLATE_{id}"; string cacheKey = $"{clientID}_TEMPLATE_{id}"; try { var factory = Factory.Instance.GetTemplate(_configuration); var template = factory.GetTemplate(clientID, id); #region update template template.Title = request.Title; template.Description = request.Description; template.Page = request.Page; template.Content = request.Content; #endregion await factory.Save(template); RemoveFromCache(cacheKey); response.StatusCode = "200"; response.Data = $"Template {id} updated with success"; return(Ok(response)); } catch (Exception ex) { response.StatusCode = "500"; response.Messages.Add(ResponseMessage.Create(ex, responseMessage)); return(StatusCode(500, response)); } }
public ActionResult <GetAccountsResponse> GetAccounts([FromHeader] string client, [FromRoute] string cpf) { GetAccountsResponse response = new GetAccountsResponse(); string responseCode = $"GET_ACCOUNTS_{client}_{cpf}"; try { var factory = WalletFactory.Instance.GetAccount(_configuration, true); var accounts = factory.GetAccounts(client, cpf); if (accounts.Count == 0) { response.StatusCode = "404"; response.Messages.Add(ResponseMessage.Create(responseCode, "no account found for this client.")); return(NotFound(response)); } response.StatusCode = "200"; accounts.ForEach(account => response.Data.Add(account.Adapt())); return(Ok(response)); } catch (Exception ex) { response.StatusCode = "500"; response.Messages.Add(ResponseMessage.Create(ex, responseCode)); return(StatusCode(500, response)); } }
public async Task <ActionResult <CreateTemplateResponse> > Post([FromRoute] string clientID, [FromBody] CreateTemplateRequest request) { CreateTemplateResponse response = new CreateTemplateResponse(); string responseMessage = $"CREATE_{clientID}_TEMPLATE_{request.Title}"; try { var dto = new TemplateDTO { ClientID = clientID, Title = request.Title, Description = request.Description, Page = request.Page, Content = request.Content }; var factory = Factory.Instance.GetTemplate(_configuration); await factory.Save(dto.Adapt()); response.StatusCode = "200"; response.Data = $"Template {request.Title} created with success"; return(Ok(response)); } catch (Exception ex) { response.StatusCode = "500"; response.Messages.Add(ResponseMessage.Create(ex, responseMessage)); return(StatusCode(500, response)); } }
public async Task <ActionResult <ManageAccountResponse> > ConsumeAccount([FromHeader] string client, [FromRoute] string id, [FromBody] ConsumeAccountRequest request) { ManageAccountResponse response = new ManageAccountResponse(); string responseCode = $"CONSUME_{client}_{id}"; try { var factory = WalletFactory.Instance.GetConsumeAccount(_configuration); var transaction = await factory.Consume(client, id, request.LocationID, request.Value); var dto = new ManageAccountDTO { TransactionID = transaction }; response.StatusCode = "200"; response.Data = dto; return(Ok(response)); } catch (Exception ex) { response.StatusCode = "500"; response.Messages.Add(ResponseMessage.Create(ex, responseCode)); return(StatusCode(500, response)); } }
public async Task <IActionResult> Login([FromBody] LoginRequest request, CancellationToken cancellationToken = default) { var response = new LoginResponse(); try { var filter = new LoginFilter(request.Company, request.CPF, request.Password); var user = await _userAppService.Login(filter, cancellationToken); if (user == null) { response.StatusCode = 404; response.Messages.Add(ResponseMessage.Create("", "Cannot find user with this parameters.")); return(NotFound(response)); } response.StatusCode = 200; response.Data = user.Token; return(Ok(response)); } catch (Exception ex) { response.StatusCode = 500; response.Messages.Add(ResponseMessage.Create(ex, "")); return(StatusCode(500, response)); } }
public async Task <ActionResult <ManageAccountsResponse> > ConsumeBalance([FromHeader] string client, [FromRoute] string cpf, [FromRoute] int accountType, [FromBody] ConsumeAccountRequest request) { ManageAccountsResponse response = new ManageAccountsResponse(); string responseCode = $"CONSUME_ACCOUNT_{client}_{accountType}_FROM_{cpf}"; try { var factory = WalletFactory.Instance.GetConsumeAccount(_configuration); var transactions = await factory.Consume(client, cpf, request.LocationID, accountType, request.Value); response.StatusCode = "200"; transactions.ForEach(transaction => response.Data.Add(new ManageAccountDTO { TransactionID = transaction })); return(Ok(response)); } catch (Exception ex) { response.StatusCode = "500"; response.Messages.Add(ResponseMessage.Create(ex, responseCode)); return(StatusCode(500, response)); } }
public async Task <ActionResult <CreateAccountResponse> > CreateVoucher([FromHeader] string client, [FromRoute] string cpf, [FromBody] CreateAccountRequest request) { CreateAccountResponse response = new CreateAccountResponse(); string responseCode = $"CREATE_VOUCHER_{client}_{cpf}"; try { var factory = WalletFactory.Instance.GetCreateAccount(_configuration); var account = await factory.Create(client, cpf, request.AccountID, request.LocationID, request.InitialValue, request.ExpiresOn, 1, request.ExtensionAttributes); var dto = new NewAccountDTO { AccountID = account.Item1, TransactionID = account.Item2 }; response.StatusCode = "200"; response.Data = dto; return(Ok(response)); } catch (Exception ex) { response.StatusCode = "500"; response.Messages.Add(ResponseMessage.Create(ex, responseCode)); return(StatusCode(500, response)); } }
public ActionResult <GetAccountResponse> Get([FromHeader] string client, [FromRoute] string id) { GetAccountResponse response = new GetAccountResponse(); string responseCode = $"GET_ACCOUNT_{client}_{id}"; try { var factory = WalletFactory.Instance.GetAccount(_configuration, true); var account = factory.GetAccount(client, id); if (account == null) { response.StatusCode = "404"; response.Messages.Add(ResponseMessage.Create(responseCode, "account not found.")); return(NotFound(response)); } response.StatusCode = "200"; response.Data = account.Adapt(); return(Ok(response)); } catch (Exception ex) { response.StatusCode = "500"; response.Messages.Add(ResponseMessage.Create(ex, responseCode)); return(StatusCode(500, response)); } }
public async Task <ActionResult <UpdateAccountResponse> > Put([FromHeader] string client, [FromRoute] string id, [FromBody] UpdateAccountRequest request) { UpdateAccountResponse response = new UpdateAccountResponse(); string responseCode = $"UPDATE_{client}_{id}"; try { var factory = WalletFactory.Instance.GetAccount(_configuration, true); var account = await factory.UpdateGiftcard(client, id, request.CPF); var dto = new UpdateAccountDTO { AccountID = account.AccountID }; response.StatusCode = "200"; response.Data = dto; return(Ok(response)); } catch (Exception ex) { response.StatusCode = "500"; response.Messages.Add(ResponseMessage.Create(ex, responseCode)); return(StatusCode(500, response)); } }
public void ResponseMessageTests() { Assert.AreEqual("<-- Ok: Something", ResponseMessage.Create("Something", null, null).ToString()); Assert.AreEqual("<-- Ok: Something #3", ResponseMessage.Create("Something", null, "3").ToString()); Assert.AreEqual("<-- Error: Too bad (123)", ResponseMessage.Create(null, new Error { Code = 123, Message = "Too bad" }, null).ToString()); }
public async Task <IActionResult> Post([FromBody] CreateUserCommand request, CancellationToken cancellationToken = default) { var response = new CreateUserResponse(); try { await _userAppService.Create(request, cancellationToken); return(StatusCode(201)); } catch (Exception ex) { response.StatusCode = 500; response.Messages.Add(ResponseMessage.Create(ex, "")); return(StatusCode(500, response)); } }
public async Task <IActionResult> ConsumeAccount([FromBody] ConsumeAccountRequest request, CancellationToken cancellationToken = default) { ConsumeAccountResponse response = new ConsumeAccountResponse(); try { await _accountsAppService.Consume(request.Command, cancellationToken); return(NoContent()); } catch (Exception ex) { response.StatusCode = 500; response.Messages.Add(ResponseMessage.Create(ex, "")); return(StatusCode(500, response)); } }
public async Task <IActionResult> RegisterGiftcard([FromBody] RegisterGiftcardRequest request, CancellationToken cancellationToken = default) { RegisterGiftcardResponse response = new RegisterGiftcardResponse(); try { var command = new RegisterGiftcardCommand(request.Company, request.CPF, request.AccountId); await _accountsAppService.Register(command, cancellationToken); return(NoContent()); } catch (Exception ex) { response.StatusCode = 500; response.Messages.Add(ResponseMessage.Create(ex, "")); return(StatusCode(500, response)); } }
public async Task <ActionResult <DeleteTemplateResponse> > Delete([FromRoute] string clientID, [FromRoute] int id) { DeleteTemplateResponse response = new DeleteTemplateResponse(); string responseMessage = $"DELETE_{clientID}_TEMPLATE_{id}"; try { var factory = Factory.Instance.GetTemplate(_configuration); await factory.Delete(clientID, id); response.StatusCode = "200"; response.Data = $"Template {id} deleted with success"; return(Ok(response)); } catch (Exception ex) { response.StatusCode = "500"; response.Messages.Add(ResponseMessage.Create(ex, responseMessage)); return(StatusCode(500, response)); } }
public async Task <IActionResult> CreateGiftcard([FromBody] CreateGiftcardRequest request, CancellationToken cancellationToken = default) { CreateGiftcardResponse response = new CreateGiftcardResponse(); try { var result = await _accountsAppService.CreateGiftcard(request.Command, cancellationToken); response.StatusCode = 200; response.Data = result; return(Ok(response)); } catch (Exception ex) { response.StatusCode = 500; response.Messages.Add(ResponseMessage.Create(ex, "")); return(StatusCode(500, response)); } }
public async Task <IActionResult> Logout(CancellationToken cancellationToken = default) { var response = new LogoutResponse(); try { // get company and email data from jwt var company = _httpContext.HttpContext.User.Claims.First(c => c.Type == "company")?.Value; var email = _httpContext.HttpContext.User.Claims.First(c => c.Type == "username")?.Value; var command = new LogoutUserCommand(company, email); await _userAppService.Logout(command, cancellationToken); return(NoContent()); } catch (Exception ex) { response.StatusCode = 500; response.Messages.Add(ResponseMessage.Create(ex, "")); return(StatusCode(500, response)); } }
public async Task <IActionResult> GetCPFVoucherAccounts([FromRoute] GetCPFAccountsRequest request, CancellationToken cancellationToken = default) { GetCPFAccountsResponse response = new GetCPFAccountsResponse(); try { var filter = new GetAccountsFilter(request.Company, request.CPF, AccountType.Voucher); var result = await _accountsAppService.GetAccounts(filter, cancellationToken); response.StatusCode = 200; response.Data = result; return(Ok(response)); } catch (Exception ex) { response.StatusCode = 500; response.Messages.Add(ResponseMessage.Create(ex, "")); return(StatusCode(500, response)); } }
public ActionResult <GetAccountBalanceResponse> GetAccountBalance([FromHeader] string client, [FromRoute] string cpf, [FromRoute] int accountType) { GetAccountBalanceResponse response = new GetAccountBalanceResponse(); string responseCode = $"GET_BALANCE_{client}_{cpf}"; try { var factory = WalletFactory.Instance.GetAccount(_configuration, true); var balance = factory.GetBalance(client, cpf, accountType); response.StatusCode = "200"; response.Data = new AccountBalanceDTO { Balance = string.Format("{0:N}", balance) }; return(Ok(response)); } catch (Exception ex) { response.StatusCode = "500"; response.Messages.Add(ResponseMessage.Create(ex, responseCode)); return(StatusCode(500, response)); } }
public ActionResult <GetTemplatesResponse> Get([FromRoute] string clientID) { GetTemplatesResponse response; string responseMessage = $"GET_{clientID}_TEMPLATES"; string cacheKey = $"{clientID}_TEMPLATES"; try { if (ExistsInCache(cacheKey)) { response = GetFromCache <GetTemplatesResponse>(cacheKey); } else { var factory = Factory.Instance.GetTemplate(_configuration); var templates = factory.GetTemplates(clientID); response = new GetTemplatesResponse { StatusCode = "200" }; templates.ForEach(template => response.Data.Add(template.Adapt())); AddToCache(cacheKey, response); } return(Ok(response)); } catch (Exception ex) { response = new GetTemplatesResponse { StatusCode = "500" }; response.Messages.Add(ResponseMessage.Create(ex, responseMessage)); return(StatusCode(500, response)); } }