public async Task <IActionResult> PutPeripheralDevice(int id, PeripheralDevice peripheralDevice)
        {
            if (id != peripheralDevice.Id)
            {
                return(BadRequest());
            }

            var devicesCount = _context.PeripheralDevice.Where(a => a.GatewayId == peripheralDevice.GatewayId).Count();

            if (devicesCount > 10)
            {
                return(BadRequest("the number of peripheral devices allowed is 10 only"));
            }

            _context.Entry(peripheralDevice).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PeripheralDeviceExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Esempio n. 2
0
        // Saves new payment to database
        public async Task <bool> Add(TblUser user)
        {
            try
            {
                await _context.TblUser.AddAsync(user);

                await _context.SaveChangesAsync();

                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
Esempio n. 3
0
        public async Task <ActionResult <TblUser> > PostTblUser(TblUser tblUser)
        {
            // If statement to accept only these card details to test the bank part of the API and accepts only Rupees or Dollars as payment currency
            if ((tblUser.CardNumber == "4568789469325478" && tblUser.ExpiryDate == "08/25" && tblUser.Cvv == "123") && (tblUser.Currency == "Rupees" || tblUser.Currency == "Dollars"))
            {
                // Adds a maximum amount that can be processed using the card based on the 2 different currencies used (acts as the amount of money available on the bank account)
                if ((tblUser.Currency == "Rupees" && tblUser.Amount <= 999) || (tblUser.Currency == "Dollars" && tblUser.Amount <= 500))
                {
                    _context.TblUser.Add(tblUser);
                    try
                    {
                        await _context.SaveChangesAsync();
                    }
                    catch (DbUpdateException)
                    {
                        if (TblUserExists(tblUser.Id))
                        {
                            _logger.LogWarning("Tried to create duplicate payment as payment ID already exists");
                            return(Conflict());
                        }
                        else
                        {
                            throw;
                        }
                    }

                    _logger.LogInformation("New payment has been successfully processed through the payment gateway");
                    return(CreatedAtAction("GetTblUser", new { id = tblUser.Id }, tblUser));
                }
                else
                {
                    _logger.LogWarning("Transaction could not be processed! There are insufficient funds in the bank account.");
                    return(BadRequest(new { message = "Transaction could not be processed! There are insufficient funds in the bank account." }));
                }
            }
            else
            {
                _logger.LogWarning("Card details entered are incorrect");
                return(BadRequest(new { message = "Card details entered are incorrect" }));
            }
        }