public async Task <IActionResult> RegisterCard( [FromBody] RegisterCardOptions options) { var result = await _card.RegisterAsync(options); return(Json(result)); }
private Result <Card> BasicValidations(RegisterCardOptions options) { if (string.IsNullOrWhiteSpace(options.CardNumber)) { return(new Result <Card>() { Code = ResultCodes.BadRequest, Message = $"Card number cannot be empty or null" }); } if (options.Type == Model.Types.CardType.Undefined) { return(new Result <Card>() { Code = ResultCodes.BadRequest, Message = $"Card type cannot be {options.Type}" }); } if (string.IsNullOrWhiteSpace(options.AccountNumber)) { return(new Result <Card>() { Code = ResultCodes.BadRequest, Message = $"Account Number cannot be empty or null" }); } return(new Result <Card>() { Code = ResultCodes.Success }); }
public async Task <Result <Cards> > RegisterCardsAsync(RegisterCardOptions options) { if (string.IsNullOrWhiteSpace(options.Description)) { return new Result <Cards>() { Code = ResultCodes.BadRequest, Message = $"Prepei na doseis perigrafi!" } } ; var cards = new Cards() { Description = options.Description }; await _dbContext.AddAsync <Cards>(cards); await _dbContext.SaveChangesAsync(); return(new Result <Cards>() { Code = ResultCodes.Success, Data = cards }); }
public async Task <IActionResult> RegisterCards([FromBody] RegisterCardOptions options) { var cards = await _cards.RegisterCardsAsync(options); if (cards.Code != ResultCodes.Success) { return(StatusCode(cards.Code, cards.Message)); } return(Json(cards)); }
public async void Test_RegisterCardsAsync() { var options = new RegisterCardOptions() { Description = "my family card" }; var cards = await _cards.RegisterCardsAsync(options); Assert.NotNull(cards); }
public void Add_Card_Fail() { var options = new RegisterCardOptions() { CardNumber = "", AvailableBalance = 2500.0m }; var result = _card.Register(options); Assert.Equal(ResultCodes.Success, result.Code); }
public async void Add_Card_Success_Asyns() { var options = new RegisterCardOptions() { CardNumber = "9876411114127531", AvailableBalance = 1000.0m }; var result = await _card.RegisterAsync(options); Assert.Equal(ResultCodes.Success, result.Code); }
public async void Add_Card_Success() { var options = new RegisterCardOptions() { Active = true, AvailableBalance = 0, CardNumber = "4587-6521-7832-1111", Type = Model.Types.CardType.Credit, AccountNumber = "4792-533-1051" }; var cardResult = await _card.RegisterAsync(options); Assert.Equal(ResultCodes.Success, cardResult.Code); }
public async Task <Result <Card> > RegisterAsync(RegisterCardOptions options) { if (string.IsNullOrWhiteSpace(options.CardNumber)) { return(new Result <Card>() { Code = ResultCodes.BadRequest, Message = "Card Number cannot be empty !" }); } if (options.CardNumber.Length != 16) { return(new Result <Card>() { Code = ResultCodes.BadRequest, Message = "Card Number cannot be empty !" }); } var card = new Card() { CardNumber = options.CardNumber, AvailableBalance = options.AvailableBalance, }; try { await _dbContext.AddAsync <Card>(card); await _dbContext.SaveChangesAsync(); } catch (Exception ex) { return(new Result <Card>() { Code = ResultCodes.InternalServerError, Message = $"Failed to save card. Details: {ex.Message} - {((ex.InnerException != null) ? ex.InnerException.Message : "")}", }); } return(new Result <Card>() { Code = ResultCodes.Success, Message = "Card saved.", Data = card }); }
public async Task <Result <Card> > RegisterAsync(RegisterCardOptions options) { var validations = BasicValidations(options); if (validations.Code != ResultCodes.Success) { return(new Result <Card>() { Code = validations.Code, Message = validations.Message }); } var account = await _account.GetAccountbyNumberAsync(options.AccountNumber); var card = new Card() { Active = options.Active, CardNumber = options.CardNumber, AvailableBalance = options.AvailableBalance, Type = options.Type }; account.Data.Cards.Add(card); try { await _dbContext.SaveChangesAsync(); } catch (Exception ex) { return(new Result <Card>() { Code = ResultCodes.InternalServerError, Message = $"Fail to create new Card\n" + $"Details: {ex.Message}\n" + $"StackTrace: {ex.StackTrace}\n" + $"{((ex.InnerException != null) ? "Inner Exception: " : "")}{((ex.InnerException != null) ? ex.InnerException.Message : "")}" }); } return(new Result <Card>() { Code = ResultCodes.Success, Message = $"New Card {card.CardNumber} added successfully", Data = _mapper.Map <Card>(options) }); }
public Result <Card> Register(RegisterCardOptions options) { if (string.IsNullOrWhiteSpace(options.CardNumber)) { return(new Result <Card>() { Code = ResultCodes.BadRequest, Message = "Card Number cannot be empty !" }); } var card = new Card() { CardNumber = options.CardNumber, AvailableBalance = options.AvailableBalance, }; try { _dbContext.Add <Card>(card); _dbContext.SaveChanges(); } catch (Exception ex) { return(new Result <Card>() { Code = ResultCodes.InternalServerError, Message = $"Failed to save card. Details: {ex.Message}", }); } return(new Result <Card>() { Code = ResultCodes.Success, Message = "Card saved.", Data = card }); }