Example #1
0
        private Post[] PopulatePosts(BlogDbContext db, Models.Blog[] blogs, List <PostCategory> postCategories)
        {
            var posts   = new List <Post>();
            int counter = 0;

            foreach (var blog in blogs)
            {
                for (int i = 0; i < 20; i++)
                {
                    var categories = blog.PostCategories.ToList();
                    var title      = GetRandomSentence();
                    posts.Add(new Post
                    {
                        BlogId      = blog.Id,
                        Title       = title,
                        CategoryId  = categories[counter % categories.Count].Id,
                        Content     = GetRandomSentences(Rand.Next(10, 30)),
                        PublishDate = DateTime.Now.AddDays(Rand.Next(0, 3000) * -1),
                        UrlName     = ServerTools.GenerateUrlFriendlyString(title),
                        Published   = true
                    });
                    counter++;
                }
            }
            db.Posts.AddOrUpdate(posts.ToArray());
            db.SaveChanges();
            return(posts.ToArray());
        }
Example #2
0
        //private static int headerFileNumber = 0;
        private static string GetRandomHeader()
        {
            string[] filePaths  = Directory.GetFiles(ServerTools.Paths.MediaFolderPath("Seed", "Headers"));
            int      filenumber = Rand.Next(0, filePaths.Length - 1);

            return(ServerTools.RelativePath(filePaths[filenumber]));
        }
Example #3
0
        private static string GetNextMiniature()
        {
            string[] filePaths = Directory.GetFiles(ServerTools.Paths.MediaFolderPath("Seed", "Miniatures"));

            if (miniatureFileNumber >= filePaths.Length)
            {
                miniatureFileNumber = 0;
            }
            return(ServerTools.RelativePath(filePaths[miniatureFileNumber++]));
        }
        public IHttpActionResult PostPostCategory(PostCategory postCategory)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            postCategory.BlogId  = CurrentBlogId.Value;
            postCategory.UrlSlug = ServerTools.GenerateUrlFriendlyString(postCategory.Name);
            Db.PostCategories.Add(postCategory);
            Db.SaveChanges();

            return(CreatedAtRoute("DefaultApi", new { id = postCategory.Id }, postCategory));
        }
Example #5
0
        public ActionResult Create([Bind(Include = "Title,FileSetType,urls,Description,StartDate,EndDate,SklepId")] Ulotka ulotka)
        {
            ulotka.ApplicationUserId = User.Identity.GetUserId();
            ulotka.PublishDate       = DateTime.Now;
            //ulotka.FileSetType
            ModelState.Clear();
            var urls = ulotka.urls.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

            if (TryValidateModel(ulotka) && ServerTools.TempFolderContains(urls))
            {
                try
                {
                    if (ulotka.FileSetType == FileSetType.Pdf)
                    {
                        string filePath = Path.Combine(ServerTools.TempFolderPath, urls.First());
                        ulotka.urls = ServerTools.ConvertPdfToImages(filePath);
                        System.IO.File.Delete(filePath);
                    }
                    else
                    {
                        string destinationUrlList = string.Empty;
                        foreach (var item in urls)
                        {
                            //todo better naming /sklep/ulotka123
                            string sourcePath          = Path.Combine(ServerTools.TempFolderPath, item);
                            string folder              = ulotka.FileSetType == FileSetType.Pdf ? "Data/Pdf" : "Data/Images";
                            string destinationFilePath = Path.Combine(ServerTools.MediaFolderPath(folder), item);
                            System.IO.File.Move(sourcePath, destinationFilePath);
                            destinationUrlList += ServerTools.RelativePath(destinationFilePath) + ";";
                        }
                        ulotka.urls = destinationUrlList;
                    }

                    db.Ulotki.Add(ulotka);
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
                catch (Exception)
                {
                    string key = "urls";
                    if (ModelState.ContainsKey(key))
                    {
                        ModelState.Remove(key);
                    }
                    ModelState.AddModelError(key, "Wybrane pliki nie są wspierane.");
                }
            }

            ViewBag.SklepId = new SelectList(db.Sklepy, "Id", "Id", ulotka.SklepId);
            return(View(ulotka));
        }
Example #6
0
 public ActionResult Create([Bind(Include = "Id,LogoUrl,KategoriaId")] Sklep sklep)
 {
     if (ModelState.IsValid && ServerTools.TempFolderContains(sklep.LogoUrl))
     {
         string sourcePath      = Path.Combine(ServerTools.TempFolderPath, sklep.LogoUrl);
         string destinationPath = Path.Combine(ServerTools.MediaFolderPath("Logos"), sklep.LogoUrl);
         System.IO.File.Move(sourcePath, destinationPath);
         //todo server side image resize
         sklep.LogoUrl = ServerTools.RelativePath(destinationPath);
         db.Sklepy.Add(sklep);
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     //todo chech if logo uploaded
     ViewBag.KategoriaId = new SelectList(db.Kategorie, "Id", "Id");
     return(View(sklep));
 }
Example #7
0
        public IHttpActionResult PutPost(int id, CreatePost post)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != post.Id)
            {
                return(BadRequest());
            }

            var postDb = Db.Posts.FirstOrDefault(p => p.Id == post.Id && p.BlogId == CurrentBlogId);

            if (postDb == null)
            {
                return(BadRequest("Post not found"));
            }

            Mapper.Map(post, postDb);
            postDb.UrlName = ServerTools.GenerateUrlFriendlyString(postDb.Title);


            Db.Entry(postDb).State = EntityState.Modified;

            try
            {
                Db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PostExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Example #8
0
        public IHttpActionResult PostPost(CreatePost post)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            Models.Post postModel = Mapper.Map <Models.Post>(post);

            if (postModel.Published)
            {
                postModel.PublishDate = DateTime.Now;
            }

            postModel.BlogId  = (int)CurrentUser.CurrentBlogId;
            postModel.UrlName = ServerTools.GenerateUrlFriendlyString(postModel.Title);
            //todo create tags

            Db.Posts.Add(postModel);
            Db.SaveChanges();

            return(CreatedAtRoute("DefaultApi", new { id = post.Id }, post));
        }
Example #9
0
        public bool CreateBlog(CreateBlogViewModel viewModel, string userId)
        {
            if (viewModel.UrlName.ToLower() != ServerTools.GenerateUrlFriendlyString(viewModel.UrlName))
            {
                _modelState.AddModelError("UrlName", "Url name may only consist of letters, numbers and dashes.");
            }

            if (!_modelState.IsValid ||
                (viewModel.MiniatureUrl != null && !ServerTools.Paths.TempFolderContains(viewModel.MiniatureUrl)))
            {
                return(false);
            }



            Models.Blog blog = Mapper.Map <Models.Blog>(viewModel);
            blog.UserId = userId;

            string blogFolderPath = CreateBlogFolder(blog.UrlName);

            if (viewModel.MiniatureUrl != null)
            {
                bool   invalidImage         = false;
                string tempImgPath          = Path.Combine(ServerTools.Paths.TempFolder, viewModel.MiniatureUrl);
                string imageDestinationPath = "";
                using (var img = Image.FromFile(tempImgPath))
                {
                    if (!ValidateImage(img))
                    {
                        invalidImage = true;
                    }
                    else
                    {
                        imageDestinationPath = ResizeAndSave(img, viewModel.MiniatureUrl, blogFolderPath);
                    }
                }
                File.Delete(tempImgPath);
                if (invalidImage)
                {
                    return(false);
                }
                blog.MiniatureUrl = ServerTools.RelativePath(imageDestinationPath);
            }
            else
            {
                blog.MiniatureUrl = "/MediaData/Default/miniature.jpg";
            }

            var layoutSettings = new LayoutSettings();

            /*_db.LayoutSettings.Add(layoutSettings);*/
            blog.LayoutSettings = layoutSettings;

            _db.Blogs.Add(blog);
            _db.SaveChanges();

            var postCategory = new PostCategory {
                Name = "General", UrlSlug = "general", BlogId = blog.Id
            };

            _db.PostCategories.Add(postCategory);

            var user = _db.Users.Find(userId);

            if (user.CurrentBlogId == null)
            {
                user.CurrentBlogId    = blog.Id;
                _db.Entry(user).State = EntityState.Modified;
            }
            _db.SaveChanges();

            return(true);
        }
Example #10
0
        public IHttpActionResult PutBlog(bool currentBlog, bool editHeader, EditBlogHeader header)
        {
            var blogId = CurrentUser.CurrentBlogId;

            if (CurrentUser.CurrentBlogId == null)
            {
                return(NotFound());
            }

            var blog = db.Blogs.Find(blogId);

            if (blog == null)
            {
                return(NotFound());
            }

            byte[] imageBytes = System.Convert.FromBase64String(header.File);

            Image image;

            using (MemoryStream ms = new MemoryStream(imageBytes))
            {
                image = Image.FromStream(ms);
            }
            int w = 1140;
            int h = 279;

            var resized = Imager.Crop(image, new Rectangle((int)header.X, (int)header.Y, w, h));

            string fileName = "header." + header.FileName.Split('.').Last();
            string path     = ServerTools.Paths.MediaFolderBlogsPath(blog.UrlName, fileName);

            if (File.Exists(path))
            {
                File.Delete(path);
            }
            resized.Save(path);

            blog.HeaderUrl = ServerTools.RelativePath(path);

            db.Entry(blog).State = EntityState.Modified;
            db.SaveChanges();

            return(Ok(new{ HeaderUrl = blog.HeaderUrl }));

            /*if (!ModelState.IsValid)
             * {
             *  return BadRequest(ModelState);
             * }*/
/*
 *          if (id != blog.Id)
 *          {
 *              return BadRequest();
 *          }
 *
 *          Models.Blog blogModel = db.Blogs.Find(id);
 *
 *          if (blogModel == null)
 *              return NotFound();
 *
 *          if (!BelongsToCurrentUser(blogModel))
 *              return Unauthorized();
 *
 *          Mapper.Map(blog, blogModel);
 *
 *          db.Entry(blogModel).State = EntityState.Modified;
 *
 *          try
 *          {
 *              db.SaveChanges();
 *          }
 *          catch (DbUpdateConcurrencyException)
 *          {
 *              if (!BlogExists(id))
 *              {
 *                  return NotFound();
 *              }
 *              else
 *              {
 *                  throw;
 *              }
 *          }*/

            //  return StatusCode(HttpStatusCode.NoContent);
        }