Пример #1
0
        public async Task <IActionResult> Create(ProgramDetailViewModel model)
        {
            if (!string.IsNullOrWhiteSpace(model.Program.JoinBadgeName) ||
                !string.IsNullOrWhiteSpace(model.BadgeMakerImage) ||
                model.BadgeUploadImage != null)
            {
                if (string.IsNullOrWhiteSpace(model.Program.JoinBadgeName))
                {
                    ModelState.AddModelError("Program.JoinBadgeName", "Please provide a name for the badge");
                }
                if (string.IsNullOrWhiteSpace(model.BadgeMakerImage) && model.BadgeUploadImage == null)
                {
                    ModelState.AddModelError("BadgePath", "Please provide an image for the badge.");
                }
                else if (model.BadgeUploadImage != null &&
                         (string.IsNullOrWhiteSpace(model.BadgeMakerImage) || !model.UseBadgeMaker) &&
                         (Path.GetExtension(model.BadgeUploadImage.FileName).ToLower() != ".jpg" &&
                          Path.GetExtension(model.BadgeUploadImage.FileName).ToLower() != ".jpeg" &&
                          Path.GetExtension(model.BadgeUploadImage.FileName).ToLower() != ".png"))
                {
                    ModelState.AddModelError("BadgeUploadImage", "Please use a .jpg or .png image.");
                }
            }

            if (model.Program.AgeMaximum < model.Program.AgeMinimum)
            {
                ModelState.AddModelError("Program.AgeMaximum", "The maximum age cannot be lower than the minimum age.");
            }

            if (ModelState.IsValid)
            {
                try
                {
                    if (model.BadgeUploadImage != null ||
                        !string.IsNullOrWhiteSpace(model.BadgeMakerImage))
                    {
                        byte[] badgeBytes;
                        string filename;
                        if (!string.IsNullOrWhiteSpace(model.BadgeMakerImage) &&
                            (model.BadgeUploadImage == null || model.UseBadgeMaker))
                        {
                            var badgeString = model.BadgeMakerImage.Split(',').Last();
                            badgeBytes = Convert.FromBase64String(badgeString);
                            filename   = "badge.png";
                        }
                        else
                        {
                            using (var fileStream = model.BadgeUploadImage.OpenReadStream())
                            {
                                using (var ms = new MemoryStream())
                                {
                                    fileStream.CopyTo(ms);
                                    badgeBytes = ms.ToArray();
                                }
                            }
                            filename = Path.GetFileName(model.BadgeUploadImage.FileName);
                        }
                        var newBadge = new Badge
                        {
                            Filename = filename
                        };
                        var badge = await _badgeService.AddBadgeAsync(newBadge, badgeBytes);

                        model.Program.JoinBadgeId   = badge.Id;
                        model.Program.JoinBadgeName = model.Program.JoinBadgeName.Trim();
                    }

                    model.Program.AskAge         = model.AgeValues >= 1;
                    model.Program.AgeRequired    = model.AgeValues == 2;
                    model.Program.AskSchool      = model.SchoolValues >= 1;
                    model.Program.SchoolRequired = model.SchoolValues == 2;
                    model.Program.Name           = model.Program.Name.Trim();
                    var program = await _siteService.AddProgramAsync(model.Program);

                    ShowAlertSuccess($"Added Program \"{program.Name}\"!");
                    return(RedirectToAction(nameof(Edit), new { id = program.Id }));
                }
                catch (GraException gex)
                {
                    ShowAlertDanger("Unable to add Program: ", gex);
                }
            }

            var dailyLiteracyTipList = await _dailyLiteracyTipService.GetListAsync();

            var pointTranslationList = await _pointTranslationService.GetListAsync();

            model.DailyLiteracyTipList = new SelectList(dailyLiteracyTipList, "Id", "Name");
            model.PointTranslationList = new SelectList(pointTranslationList, "Id",
                                                        "TranslationName");
            PageTitle = "Create Program";
            return(View("Detail", model));
        }