Ejemplo n.º 1
0
        public async Task <IActionResult> PutMeasurement(int id, Measurement measurement)
        {
            if (id != measurement.Id)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
Ejemplo n.º 2
0
        public async Task <ActionResult <Person> > PutPerson(int id, Person person)
        {
            if (id != person.PersonId)
            {
                return(BadRequest());
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var dbPerson = await _context.Person.Where(p => p.Pin == person.Pin && p.PersonId != id).ToListAsync();

            if (dbPerson.Count != 0) // another person with the same pin exists
            {
                return(BadRequest(new { Code = 1, Msg = "Given pin already exists" }));
            }

            List <string> missingFields = new List <string>();

            if (person.Pin == null || person.Pin == "") // pin misses
            {
                missingFields.Add("Pin");
            }
            if (person.Name == null || person.Name == "") // name misses
            {
                missingFields.Add("Name");
            }

            if (missingFields.Count() > 0) // error(s) exist(s)
            {
                return(BadRequest(new { Code = 2, Msg = "Missing or empty fields", Fields = missingFields.ToArray() }));
            }

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

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

            return(person);
        }
Ejemplo n.º 3
0
        public async Task <ActionResult <List <Measurement> > > PostMeasurement([FromRoute] int personId, List <Measurement> measurements)
        {
            foreach (var measurement in measurements)
            {
                // check that person ids match
                if (personId != measurement.PersonId)
                {
                    return(BadRequest());
                }
            }

            List <string> missingFields = new List <string>();

            foreach (var measurement in measurements)
            {
                if (measurement.UnitName == null || measurement.UnitName == "") // unit name missing
                {
                    if (!missingFields.Contains("UnitName"))
                    {
                        // add the field to the list if not yet exists
                        missingFields.Add("UnitName");
                    }
                }
            }

            if (missingFields.Count() > 0) // error(s) exist(s)
            {
                return(BadRequest(new { Code = 1, Msg = "Missing or empty fields", Fields = missingFields.ToArray() }));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _context.Measurement.AddRange(measurements);
            await _context.SaveChangesAsync();

            return(measurements);
        }
Ejemplo n.º 4
0
        public async Task Add(Measurement entity)
        {
            await _dbContext.Measurements.AddAsync(entity);

            await _dbContext.SaveChangesAsync();
        }
        public async Task AddToDbAsync(Measurement measurement)
        {
            await context.Measurements.AddAsync(measurement);

            await context.SaveChangesAsync();
        }