Ejemplo n.º 1
0
        public async Task <IActionResult> QuantityRegistry([FromRoute] long indicatorId,
                                                           [FromBody] QuantityRegistry registry)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // Don't add the registry if it has the same name as other registry on this indicator
            List <Registry> registries = await _context.Registries.Where(r => r.IndicatorID == indicatorId).ToListAsync();

            foreach (Registry r in registries)
            {
                if (r.Name.ToUpper().Trim().Equals(registry.Name.ToUpper().Trim()) && r.RegistryID != indicatorId)
                {
                    return(NoContent());
                }
            }

            Indicator indicator = await _context.Indicators.SingleAsync(i => i.IndicatorID == indicatorId);

            indicator.Registries.Add(registry);

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

            try
            {
                await _context.SaveChangesAsync();

                var createdRegistry = await _context.Registries.SingleOrDefaultAsync(m => m.RegistryID == registry.RegistryID);

                return(Ok(createdRegistry));
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!IndicatorExists(indicatorId))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> PutRegistry([FromRoute] long indicator, [FromRoute] long id, [FromBody] QuantityRegistry registry)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var registries = await _context.Registries.Where(r => r.IndicatorID == indicator && r.Name == registry.Name && r.RegistryID != id).ToListAsync();

            if (registries.Any())
            {
                return(NoContent());
            }

            if (id != registry.RegistryID)
            {
                return(BadRequest());
            }

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

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

            return(Ok(registry));
        }