Beispiel #1
0
        public async Task <OperationDetails> CreatePublication(CreatePublicationData publicationData)
        {
            UserProfile author = repo.Profiles.Find(p => p.User.UserName == publicationData.Author).FirstOrDefault();

            if (author != null)
            {
                var newPublication = new Publication()
                {
                    Title           = publicationData.Title,
                    Content         = publicationData.Content,
                    PublicationDate = DateTime.Now,
                    ContentType     = (Model.Enums.ContentType)Enum.Parse(typeof(Model.Enums.ContentType), Enum.GetName(typeof(ContentType), publicationData.ContentType)),
                    AuthorId        = author.AccountId
                };

                repo.Publications.Create(newPublication);
                await repo.SaveChangesAsync();

                return(new OperationDetails(true, "New publication was created successfully"));
            }
            return(new OperationDetails(false, "Can`t find author in database"));
        }
        public async Task <ActionResult> Add(NewPublicationViewModel model)
        {
            byte[] content = null; //array that will contain bytes of content
            if (model.ContentType == ContentType.Image)
            {
                //TODO: Bad code. Need holder for image types names
                if (Request.Files.Count > 0 && (Request.Files[0].ContentType == System.Net.Mime.MediaTypeNames.Image.Gif || Request.Files[0].ContentType == System.Net.Mime.MediaTypeNames.Image.Jpeg))
                {
                    content = FileUploader.UploadFile(Request.Files[0]); //Convert uploaded file to byte array
                }
                else //if file is empty or has unsupported type
                {
                    ModelState.AddModelError("ImageFile", "For image post you must upload image file"); //Add validation error to model
                    return(View(model));
                }
            }
            else //if puplication has text content
            {
                content = Encoding.Default.GetBytes(model.Text); //convert text content to byte array
            }

            var publication = new CreatePublicationData() //create new publication object that contains main data to add to source
            {
                Author      = User.Identity.Name,
                Content     = content,
                ContentType = model.ContentType,
                Title       = model.Title
            };
            var result = await ContentService.CreatePublication(publication); //trying to add publication data to source

            if (result.Succedeed)                                             //if data was successfully added
            {
                return(RedirectToRoute(new { controller = "Home", action = "Hot" }));
            }

            ModelState.AddModelError("", "Cant`create publication: " + result.Message); //add validation error to model
            return(View());
        }