Example #1
0
        public async Task <ActionResult <ReleaseViewModel> > CreateReleaseAsync(CreateReleaseViewModel release,
                                                                                Guid publicationId)
        {
            release.PublicationId = publicationId;

            return(await _releaseService
                   .CreateReleaseAsync(release)
                   .HandleFailuresOrOk());
        }
Example #2
0
        public async Task <Either <ActionResult, ReleaseViewModel> > CreateReleaseAsync(CreateReleaseViewModel createRelease)
        {
            return(await _persistenceHelper
                   .CheckEntityExists <Publication>(createRelease.PublicationId)
                   .OnSuccess(_userService.CheckCanCreateReleaseForPublication)
                   .OnSuccess(async _ => await ValidateReleaseSlugUniqueToPublication(createRelease.Slug, createRelease.PublicationId))
                   .OnSuccess(async() =>
            {
                var release = _mapper.Map <Release>(createRelease);

                release.Id = _guidGenerator.NewGuid();

                if (createRelease.TemplateReleaseId.HasValue)
                {
                    CreateGenericContentFromTemplate(createRelease.TemplateReleaseId.Value, release);
                }
                else
                {
                    release.GenericContent = new List <ContentSection>();
                }

                release.SummarySection = new ContentSection
                {
                    Type = ContentSectionType.ReleaseSummary
                };
                release.KeyStatisticsSection = new ContentSection {
                    Type = ContentSectionType.KeyStatistics
                };
                release.KeyStatisticsSecondarySection = new ContentSection {
                    Type = ContentSectionType.KeyStatisticsSecondary
                };
                release.HeadlinesSection = new ContentSection {
                    Type = ContentSectionType.Headlines
                };
                release.Created = DateTime.UtcNow;
                release.CreatedById = _userService.GetUserId();

                await _context.Releases.AddAsync(release);
                await _context.SaveChangesAsync();
                return await GetRelease(release.Id);
            }));
        }
Example #3
0
        public IActionResult CreateRelease(CreateReleaseViewModel model)
        {
            FluentReuiredIfAttribute validator = new FluentReuiredIfAttribute();
            ValidationResult         result    = validator.Validate(model);

            if (ModelState.IsValid && result.IsValid)
            {
                IRelease release = new Release
                {
                    Description = model.Description,
                    ImgLocation = model.ImgLocation,
                    ReleaseDate = model.ReleaseDate,
                    Title       = model.Title,
                    Category    = new Category
                    {
                        Id = model.CategoryId,
                    },
                    User = new User
                    {
                        Id = HttpContext.Session.GetInt32(SessionHolder.SessionUserId).GetValueOrDefault()
                    }
                };
                IFormFile file = model.ImgFile;
                try {
                    if (ImageHandler.IsImageValid(file))
                    {
                        release.ImgLocation = ImageHandler.SaveImage(he.WebRootPath, file);
                    }
                }

                catch (BadImageFormatException ex) {
                    ModelState.AddModelError("ImgFile", ex.Message);
                }

                catch (FileLoadException ex) {
                    ModelState.AddModelError("ImgFile", ex.Message);
                }


                foreach (ValidationFailure error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.ErrorMessage);
                }

                if (ModelState.ErrorCount == 0)
                {
                    try {
                        releaseLogic.Add(release);
                    }

                    catch (SqlException ex) {
                        if (ex.Number == (int)SqlErrorCodes.InsertRolledback)
                        {
                            ModelState.AddModelError(string.Empty, "You have reached your daily creation limit");
                        }
                        else
                        {
                            ModelState.AddModelError(string.Empty, "Something went wrong in the database");
                        }
                    }
                }
            }
            if (ModelState.ErrorCount == 0)
            {
                return(RedirectToAction("index"));
            }
            return(View("Create", model));
        }