Esempio n. 1
0
        public async Task <IHttpActionResult> Update(LogModel model)
        {
            try
            {
                var log = await _logRepository.FindByLogIdAsync(model.LogId);

                if (log == null)
                {
                    return(BadRequest(ResponseMessages.LogNotFound.ToDesc()));
                }

                if (log.Name != model.Name && await _logRepository.LogAlreadyExistsAsync(model.Name, log.UserId))
                {
                    return(BadRequest(ResponseMessages.LogNameDuplicate.ToDesc()));
                }

                log.WidgetColor  = model.WidgetColor;
                log.Name         = model.Name;
                log.DateModified = AppTime.Now();

                await _logRepository.UpdateAsync(log);

                var notification = CreateNotification(log, model);
                await _notificationRepository.CreateOrUpdate(notification);

                var isExist = await _notificationRepository.IsUserSubscribedToDigestAsync(notification.UserId);

                if (isExist)
                {
                    _jobRunner.CreateDigestEmailJobIfNotExists(notification);
                }
                else
                {
                    _jobRunner.DeleteDigestEmailJobIfExists(notification);
                }

                this.LogInfo(IsDbUser
                    ? $"{CurrentUser.FullName} updatd log with id {model.LogId}"
                    : $"{Auth0User.Email} updated log with id {model.LogId}");

                return(Ok(ResponseMessages.LogAdded.ToDesc()));
            }
            catch (Exception ex)
            {
                this.LogInfo($"{CurrentUserId} tried to updated log with error:  {ex.InnerException.Message}");
            }
            return(InternalServerError());
        }