Ejemplo n.º 1
0
        public async Task <IActionResult> PutSpeaker(int id, UserViewModel userVM)
        {
            var existingUser = _context.Users.Where(u => u.id == id).FirstOrDefault();

            if (existingUser != null)
            {
                existingUser.name  = userVM.name;
                existingUser.email = userVM.email;
            }
            else
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
        public ActionResult <object> PutSeminar(int id, SeminarViewModel seminarVM)
        {
            var message    = "Updated " + seminarVM.name + " successfully!";
            var stringDate = seminarVM.Date + " " + seminarVM.Time + ":00";
            var day        = new Day {
                day = Convert.ToDateTime(stringDate)
            };

            var speaker         = new Speaker();
            var existingSpeaker = _context.Speakers.Where(s => s.name == seminarVM.SpeakersName).FirstOrDefault();
            var existingSeminar = _context.Seminars.Where(s => s.id == id).FirstOrDefault();

            if (existingSpeaker == null)
            {
                speaker.name = seminarVM.SpeakersName;
                _context.Speakers.Add(speaker);
            }
            else
            {
                speaker = existingSpeaker;
            }

            if (existingSeminar != null)
            {
                existingSeminar.name        = seminarVM.name;
                existingSeminar.description = seminarVM.description;
                existingSeminar.speaker     = speaker;
                existingSeminar.day         = day;
            }
            else
            {
                return(BadRequest());
            }
            _context.Entry(existingSeminar).State = EntityState.Modified;

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

            return(Ok(new { message }));
        }