Esempio n. 1
0
        public async Task <ActionResult <Account> > PostAccount(Account account)
        {
            _context.Account.Add(account);
            await _context.SaveChangesAsync();

            return(CreatedAtAction(nameof(GetAccount), new { id = account.AccountId }, account));
        }
Esempio n. 2
0
        public async Task <ActionResult <Account> > PostMeterReading(MeterReading meterReading)
        {
            _context.MeterReading.Add(meterReading);

            // Check the meterreadvalue is in the format NNNNN
            Regex rgx = new Regex(@"^\d{5}$");

            if (rgx.IsMatch(meterReading.MeterReadValue))
            {
                // Check if the accountId exists
                if (_context.Account.Any(o => o.AccountId == meterReading.AccountId))
                {
                    // Check if the meter read value already exists. AccountId and Value create a unique record.
                    if ((_context.MeterReading.Any(m => m.AccountId == meterReading.AccountId && m.MeterReadValue == meterReading.MeterReadValue)) == false)
                    {
                        await _context.SaveChangesAsync();

                        return(CreatedAtAction(nameof(GetMeterReading), new { id = meterReading.AccountId }, meterReading));
                    }
                }
            }
            return(BadRequest());
        }