Exemplo n.º 1
0
        public async Task <Tuple <bool, string> > AddChatTheme(AddChatThemeInputModel model)
        {
            var targetTheme = await this.db.ChatThemes
                              .FirstOrDefaultAsync(x => x.Name.ToUpper() == model.Name.ToUpper());

            if (targetTheme != null)
            {
                return(Tuple.Create(
                           false,
                           string.Format(ErrorMessages.ChatThemeAlreadyExist, model.Name.ToUpper())));
            }

            targetTheme = new ChatTheme
            {
                Name = model.Name,
            };
            var imageUrl = await ApplicationCloudinary.UploadImage(
                this.cloudinary,
                model.Image,
                string.Format(GlobalConstants.ChatThemeName, targetTheme.Id),
                GlobalConstants.ChatThemesFolderName);

            targetTheme.Url = imageUrl;

            this.db.ChatThemes.Add(targetTheme);
            await this.db.SaveChangesAsync();

            return(Tuple.Create(
                       true,
                       string.Format(SuccessMessages.SuccessfullyAddedChatTheme, model.Name.ToUpper())));
        }
        public async Task <IActionResult> AddChatTheme(AddChatThemeInputModel model)
        {
            if (this.ModelState.IsValid)
            {
                Tuple <bool, string> result = await this.addChatThemeService.AddChatTheme(model);

                if (!result.Item1)
                {
                    this.TempData["Error"] = result.Item2;
                    return(this.RedirectToAction("Index", "AddChatTheme", model));
                }

                this.TempData["Success"] = result.Item2;
                return(this.RedirectToAction("Index", "AddChatTheme"));
            }
            else
            {
                this.TempData["Error"] = ErrorMessages.InvalidInputModel;
                return(this.RedirectToAction("Index", "AddChatTheme", model));
            }
        }