Ejemplo n.º 1
0
        public async Task <IActionResult> Create([FromBody] CreateTask dto)
        {
            if (dto == null)
            {
                return(BadRequest());
            }

            try
            {
                dto.UserId = IdentityHelper.GetUserId(User);
            }
            catch (UnauthorizedAccessException)
            {
                return(Unauthorized());
            }

            CreatedTask createdTask = await _taskService.CreateAsync(dto, _createValidator);

            // Notify
            var usersToBeNotified = await _userService.GetToBeNotifiedOfListChangeAsync(dto.ListId, dto.UserId, dto.IsPrivate.HasValue&& dto.IsPrivate.Value);

            if (usersToBeNotified.Any())
            {
                var currentUser = await _userService.GetAsync(dto.UserId);

                SimpleList list = await _listService.GetAsync(dto.ListId);

                foreach (var user in usersToBeNotified)
                {
                    CultureInfo.CurrentCulture = new CultureInfo(user.Language, false);
                    var message = _localizer["CreatedTaskNotification", IdentityHelper.GetUserName(User), createdTask.Name, list.Name];

                    var createNotificationDto = new CreateOrUpdateNotification(user.Id, dto.UserId, list.Id, createdTask.Id, message);
                    var notificationId        = await _notificationService.CreateOrUpdateAsync(createNotificationDto);

                    var pushNotification = new PushNotification
                    {
                        SenderImageUri = currentUser.ImageUri,
                        UserId         = user.Id,
                        Application    = "To Do Assistant",
                        Message        = message,
                        OpenUrl        = GetNotificationsPageUrl(notificationId)
                    };

                    _senderService.Enqueue(pushNotification);
                }
            }

            return(StatusCode(201, createdTask.Id));
        }
        public async Task EnqueueRegisterConfirmationEmailAsync(string toName, string toEmail, Uri url, string language)
        {
            var email = new Email
            {
                ToAddress = toEmail,
                ToName    = toName,
                Subject   = _localizer["RegisterConfirmationEmailSubject", _applicationName]
            };

            string bodyText = await GetTemplateContentsAsync("RegisterConfirmation.txt", language);

            email.BodyText = bodyText
                             .Replace("#NAME#", toName, StringComparison.Ordinal)
                             .Replace("#URL#", url.AbsoluteUri, StringComparison.Ordinal);

            string bodyHtml = await GetTemplateContentsAsync("RegisterConfirmation.html", language);

            email.BodyHtml = bodyHtml
                             .Replace("#SUBJECT#", email.Subject, StringComparison.Ordinal)
                             .Replace("#NAME#", toName, StringComparison.Ordinal)
                             .Replace("#URL#", url.AbsoluteUri, StringComparison.Ordinal);

            _senderService.Enqueue(email);
        }
        public async Task <IActionResult> Update([FromBody] UpdateRecipe dto)
        {
            if (dto == null)
            {
                return(BadRequest());
            }

            try
            {
                dto.UserId = IdentityHelper.GetUserId(User);
            }
            catch (UnauthorizedAccessException)
            {
                return(Unauthorized());
            }

            RecipeToNotify original = await _recipeService.UpdateAsync(dto, _updateRecipeValidator);

            // Notify
            var usersToBeNotified = await _userService.GetToBeNotifiedOfRecipeChangeAsync(original.Id, dto.UserId);

            if (usersToBeNotified.Any())
            {
                var currentUser = await _userService.GetAsync(dto.UserId);

                foreach (var user in usersToBeNotified)
                {
                    CultureInfo.CurrentCulture = new CultureInfo(user.Language, false);
                    var message = _localizer["UpdatedRecipeNotification", IdentityHelper.GetUserName(User), original.Name];

                    var pushNotification = new PushNotification
                    {
                        SenderImageUri = currentUser.ImageUri,
                        UserId         = user.Id,
                        Application    = "Cooking Assistant",
                        Message        = message
                    };

                    _senderService.Enqueue(pushNotification);
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> Update([FromBody] UpdateList dto)
        {
            if (dto == null)
            {
                return(BadRequest());
            }

            try
            {
                dto.UserId = IdentityHelper.GetUserId(User);
            }
            catch (UnauthorizedAccessException)
            {
                return(Unauthorized());
            }

            UpdateListOriginal original = await _listService.UpdateAsync(dto, _updateValidator);

            // Notify
            var usersToBeNotified = await _userService.GetToBeNotifiedOfListChangeAsync(original.Id, dto.UserId);

            if (usersToBeNotified.Any())
            {
                var currentUser = await _userService.GetAsync(dto.UserId);

                if (dto.Name != original.Name || dto.Icon != original.Icon)
                {
                    var resourceKey = "UpdatedListNotification";

                    if (dto.Name != original.Name && dto.Icon == original.Icon)
                    {
                        resourceKey = "UpdatedListNameNotification";
                    }
                    else if (dto.Name == original.Name && dto.Icon != original.Icon)
                    {
                        resourceKey = "UpdatedListIconNotification";
                    }

                    foreach (var user in usersToBeNotified)
                    {
                        CultureInfo.CurrentCulture = new CultureInfo(user.Language, false);
                        var message = _localizer[resourceKey, IdentityHelper.GetUserName(User), original.Name];

                        var createNotificationDto = new CreateOrUpdateNotification(user.Id, dto.UserId, original.Id, null, message);
                        var notificationId        = await _notificationService.CreateOrUpdateAsync(createNotificationDto);

                        var pushNotification = new PushNotification
                        {
                            SenderImageUri = currentUser.ImageUri,
                            UserId         = user.Id,
                            Application    = "To Do Assistant",
                            Message        = message,
                            OpenUrl        = GetNotificationsPageUrl(notificationId)
                        };

                        _senderService.Enqueue(pushNotification);
                    }
                }
            }

            return(NoContent());
        }