Ejemplo n.º 1
0
        public async Task <IActionResult> PutCuca(
            [FromRoute] int id,
            [FromBody] Cuca cuca,
            CancellationToken cancellationToken)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != cuca.Id)
            {
                return(BadRequest());
            }

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

            foreach (var participant in cuca.Participants)
            {
                bool userExists = await UserExists(participant.Id, cancellationToken);

                _context.Entry(participant).State = userExists ? EntityState.Modified : EntityState.Added;
            }

            try
            {
                await _context.SaveChangesAsync(cancellationToken);
            }
            catch (DbUpdateConcurrencyException)
            {
                bool cucaExists = await CucaExists(id, cancellationToken);

                if (!cucaExists)
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> PostCuca([FromBody] Cuca cuca, CancellationToken cancellationToken)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _context.Entry(cuca).State = EntityState.Added;

            foreach (var participant in cuca.Participants)
            {
                bool userExists = await UserExists(participant.Id, cancellationToken);

                _context.Entry(participant).State = userExists ? EntityState.Modified : EntityState.Added;
            }

            await _context.SaveChangesAsync(cancellationToken);

            return(CreatedAtAction("GetCuca", new { id = cuca.Id }, cuca));
        }