Пример #1
0
        public async Task <ActionResult <NotificationViewModel> > PutNotification(int id,
                                                                                  [FromBody] NotificationEditModel notificationEditModel)
        {
            var notification = await _notificationsRepository.FindByIdAsync(id);

            if (notification is null)
            {
                return(BadRequest($"No existe una notificación con el código {id}."));
            }

            _mapper.Map(notificationEditModel, notification);
            _notificationsRepository.Update(notification);

            try
            {
                await _unitWork.SaveAsync();
            }
            catch (DbUpdateException)
            {
                if (!NotificationExists(id))
                {
                    return(NotFound($"Actualizacón fallida. No existe ninguna notificación con el código {id}."));
                }

                throw;
            }

            return(_mapper.Map <NotificationViewModel>(notification));
        }
Пример #2
0
        public object Put(Notify model)
        {
            object json;

            try
            {
                model = repository.Update(model);

                json = new
                {
                    total   = 1,
                    data    = model,
                    success = true
                };
            }
            catch (Exception ex)
            {
                LogManager.Write("ERROR:" + Environment.NewLine + "\tMETHOD = " + this.GetType().FullName + "." + MethodBase.GetCurrentMethod().Name + Environment.NewLine + "\tMESSAGE = " + ex.Message);

                json = new
                {
                    message = ex.Message,
                    success = false
                };
            };

            return(json);
        }
Пример #3
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,Name,Description")] Notification notification)
        {
            if (id != notification.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var notificationDb = await _repository.GetById(id);

                    if (notificationDb == null)
                    {
                        return(NotFound());
                    }
                    notification.OwnerID = notificationDb.OwnerID;

                    if (!await CheckIfUserAuthorizedForNotification(notification, NotificatinOperations.Update))
                    {
                        return(Forbid());
                    }
                    _repository.Update(notification);
                    await _repository.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!NotificationExists(notification.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(notification));
        }