Пример #1
0
        public Result AddElement(H_Article model)
        {
            try
            {
                var result = _unitOfWork.HElementRepository.FindBy(x => x.Title.Trim().ToLower() == model.Title.Trim().ToLower());

                if (result.Any())
                {
                    return(new Result(ResultStatus.ALREADYEXISTS));
                }

                var author = _unitOfWork.AuthorRepository.FindBy(x => x.Name == model.Author.Name).FirstOrDefault();
                model.Author = model.Author.Id == 0 ? new Author {
                    Name = model.Author.Name
                } : _unitOfWork.AuthorRepository.GetById(model.Author.Id);

                if (model.Files != null)
                {
                    model.MediaElements = _generalService.CreateMediaElements(model.Files, model.DateAdded.Year, "1", ProjectBaseEnum.Main);
                }
                model.Slug = _generalService.GenerateSlug(model.Title, model.DateAdded.Year, "1");

                _unitOfWork.HElementRepository.Add(model);
                _unitOfWork.SaveChanges();

                return(new Result(ResultStatus.OK));
            }
            catch (Exception ex)
            {
                return(new Result(ResultStatus.EXCEPTION, ex.Message));
            }
        }
Пример #2
0
        public Result <Magazine> Add(Magazine model)
        {
            try
            {
                var category = _unitOfWork.MagazineCategoryRepository.GetById(model.Category.Id);
                if (category == null)
                {
                    return(new Result <Magazine>(ResultStatus.ERROR, "Hiba a kategória lekérése során"));
                }

                model.Category = category;

                var exists = _unitOfWork.MagazineRepository.FindBy(x => x.PublishYear == model.PublishYear && x.PublishSection.Contains(model.PublishSection) && x.Category.Id == model.Category.Id);
                if (exists.Any())
                {
                    return(new Result <Magazine>(ResultStatus.ALREADYEXISTS, "A lapszám már létezik! Kérem ellenőrizze az adatokat!"));
                }

                if (model.Files != null)
                {
                    ICollection <IFormFile> files = new List <IFormFile>();
                    files.Add(model.Files);
                    model.MediaElements = _generalService.CreateMediaElements(files, model.PublishYear, model.PublishSection, ProjectBaseEnum.Gyopar);
                }

                model.DateCreated = DateTime.Now;
                model.Slug        = _generalService.GenerateSlug(model.Title, model.PublishYear, model.PublishSection);
                _unitOfWork.MagazineRepository.Add(model);
                _unitOfWork.SaveChanges();
                return(new Result <Magazine>(model));
            }
            catch (Exception ex)
            {
                return(new Result <Magazine>(ResultStatus.EXCEPTION, ex.Message));
            }
        }
Пример #3
0
        public Result <Article> Add(Article model, string userName)
        {
            try
            {
                if (model.Files != null)
                {
                    var uploads = Path.Combine(_environment.WebRootPath, $"Uploads/{model.Magazine.PublishYear}/{model.Magazine.PublishSection}");
                    if (!Directory.Exists(uploads))
                    {
                        Directory.CreateDirectory(uploads);
                    }

                    var mediaElements = new List <MediaElement>();
                    foreach (var file in model.Files)
                    {
                        if (file.Length > 0)
                        {
                            using (var fileStream = new FileStream(Path.Combine(uploads, file.FileName), FileMode.Create))
                            {
                                file.CopyToAsync(fileStream);
                            }
                        }
                        var mediaElem = new MediaElement
                        {
                            OriginalName = $"{uploads}/{file.Name}",
                            Name         = _generalService.RandomString(10),
                            Type         = Data.Entities.Enums.MediaTypesEnum.Image
                        };
                        mediaElements.Add(mediaElem);
                    }

                    model.MediaElement = mediaElements;
                }

                var magCat = _unitOfWork.MagazineCategoryRepository.GetById(model.Magazine.Category.Id);
                if (magCat == null)
                {
                    return(new Result <Article>(ResultStatus.NOT_FOUND, "Folyóirat nem található"));
                }

                var magazine   = _unitOfWork.MagazineRepository.FindBy(x => x.PublishYear == model.Magazine.PublishYear && x.PublishSection.Contains(model.Magazine.PublishSection) && x.Category.Id == model.Magazine.Category.Id);
                var enumerable = magazine as IList <Magazine> ?? magazine.ToList();
                if (!enumerable.Any())
                {
                    model.Magazine.Category    = magCat;
                    model.Magazine.Title       = String.Format("{0} / {1}", model.Magazine.PublishYear, model.Magazine.PublishSection);
                    model.Magazine.Slug        = _generalService.GenerateSlug(model.Magazine.Title, model.Magazine.PublishYear, model.Magazine.PublishSection);
                    model.Magazine.DateCreated = DateTime.Now;
                }
                else
                {
                    model.Magazine = enumerable.FirstOrDefault();
                }

                var author = model.Author.Id == 0 ? new Author {
                    Name = model.Author.Name
                } : _unitOfWork.AuthorRepository.GetById(model.Author.Id);
                model.Author = author;
                if (model.Magazine != null)
                {
                    model.Slug = _generalService.GenerateSlug(model.Title, model.Magazine.PublishYear,
                                                              model.Magazine.PublishSection);
                }
                model.PublishedBy = userName;
                model.DateCreated = DateTime.Now;

                _unitOfWork.ArticleRepository.Add(model);
                _unitOfWork.SaveChanges();
                return(new Result <Article>(model));
            }
            catch (Exception ex)
            {
                return(new Result <Article>(ResultStatus.ERROR, ex.Message));
            }
        }