Пример #1
0
        public async Task <IActionResult> Index(AddHolidayThemeInputModel model)
        {
            if (this.ModelState.IsValid)
            {
                Tuple <bool, string> result = await this.addHolidayThemeService.AddNewHolidayTheme(model);

                if (!result.Item1)
                {
                    this.TempData["Error"] = result.Item2;
                    return(this.View());
                }

                this.TempData["Success"] = result.Item2;
                return(this.RedirectToAction("Index", "AddHolidayTheme"));
            }

            return(this.View());
        }
Пример #2
0
        public async Task <Tuple <bool, string> > AddNewHolidayTheme(AddHolidayThemeInputModel model)
        {
            var targetTheme = await this.db.HolidayThemes
                              .FirstOrDefaultAsync(x => x.Name.ToUpper() == model.Name.ToUpper());

            if (targetTheme == null)
            {
                targetTheme = new HolidayTheme
                {
                    Name     = model.Name,
                    IsActive = false,
                };

                foreach (var icon in model.Icons)
                {
                    var targetIcon = new HolidayIcon
                    {
                        Name = Path.GetFileNameWithoutExtension(icon.FileName),
                    };

                    var iconUrl = await ApplicationCloudinary.UploadImage(
                        this.cloudinary,
                        icon,
                        string.Format(GlobalConstants.HolidayIconName, targetIcon.Id),
                        GlobalConstants.HolidayThemesFolder);

                    targetIcon.Url = iconUrl;
                    targetTheme.HolidayIcons.Add(targetIcon);
                }

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

                return(Tuple.Create(
                           true,
                           string.Format(SuccessMessages.SuccessfullyAddedHolidayTheme, targetTheme.Name.ToUpper())));
            }

            return(Tuple.Create(
                       false,
                       string.Format(ErrorMessages.HolidayThemeAlreadyExist, model.Name.ToUpper())));
        }