private async Task <Photo> UploadPhoto(IFormFile file) { var uploadResult = new ImageUploadResult(); string url; if (file.Length > 0) { using (var stream = file.OpenReadStream()) { var uploadParams = new ImageUploadParams() { File = new FileDescription(file.Name, stream), Folder = "journalApp" }; uploadResult = await _cloudinary.UploadAsync(uploadParams); } url = uploadResult.Uri.ToString(); string photoUrl = url.Replace(CloudinarySettings.url, ""); CloudinarySettings.Transformations(photoUrl, out string urlHuge, out string urlLarge, out string urlBig, out string urlMedium, out string urlSmall); return(new Photo { UrlHuge = urlHuge, UrlLarge = urlLarge, UrlBig = urlBig, UrlMedium = urlMedium, UrlSmall = urlSmall }); } else { return(null); } }
private void AddNews(string section, List <string> imgs, DateTime dateTime) { News news; int numberOfDummyNews = 10; Random r = new Random(); imgs.Shuffle(); for (int i = 0; i < numberOfDummyNews; i++) { dateTime = dateTime.Add(new TimeSpan(days: 0, hours: r.Next(0, 15), minutes: r.Next(0, 100), seconds: r.Next(0, 100))); string title; do { title = Lorem.Sentence(6); } while (title.Length < 35); string shortTitle; do { shortTitle = Lorem.Sentence(5); } while (shortTitle.Length < 25); news = new News() { ShortTitle = shortTitle, Title = title, Heading = Lorem.Sentence(11), Content = Lorem.Paragraph(35), Section = section, AddedAt = dateTime, IsImportant = true, AuthorId = r.Next(2, 7) }; CloudinarySettings.Transformations(imgs[i], out string urlHuge, out string urlLarge, out string urlBig, out string urlMedium, out string urlSmall); var photo = new Photo() { UrlHuge = urlHuge, UrlLarge = urlLarge, UrlBig = urlBig, UrlMedium = urlMedium, UrlSmall = urlSmall, NewsId = i, News = news }; var comments = new List <Comment>(); var x = r.Next(0, 10) - 4; if (x > 0) { for (int z = 0; z < x; z++) { var comment = new Comment() { AuthorId = r.Next(5, 20), Content = Lorem.Sentence(12), NewsId = i, News = news }; _dbContext.Add <Comment>(comment); comments.Add(comment); } } news.Photo = photo; news.Comments = comments; _dbContext.Add <News>(news); _dbContext.Add <Photo>(photo); } }
public void InitializeNews() { if (!_dbContext.News.Any()) { News news; int numberOfDummyNews = 150; Random r = new Random(); DateTime dateTime = new DateTime(2019, 04, 01, 0, 0, 0); List <string> sections = new List <string>() { "poland", "sport", "business", "world", "policy" }; CloudinarySettings.Transformations(_dummyPhoto, out string urlHuge, out string urlLarge, out string urlBig, out string urlMedium, out string urlSmall); for (int i = 0; i < numberOfDummyNews; i++) { dateTime = dateTime.Add(new TimeSpan(days: 0, hours: r.Next(0, 15), minutes: r.Next(0, 100), seconds: r.Next(0, 100))); string title; do { title = Lorem.Sentence(6); } while (title.Length < 35); string shortTitle; do { shortTitle = Lorem.Sentence(5); } while (shortTitle.Length < 25); news = new News() { ShortTitle = shortTitle, Title = title, Heading = Lorem.Sentence(11), Content = Lorem.Paragraph(35), Section = sections[r.Next(0, 5)], AddedAt = dateTime, IsImportant = r.Next(0, 7) > 2 ? true : false, AuthorId = r.Next(2, 7) }; var photo = new Photo() { UrlHuge = urlHuge, UrlLarge = urlLarge, UrlBig = urlBig, UrlMedium = urlMedium, UrlSmall = urlSmall, NewsId = i, News = news }; var comments = new List <Comment>(); var x = r.Next(0, 10) - 4; if (x > 0) { for (int z = 0; z < x; z++) { var comment = new Comment() { AuthorId = r.Next(5, 20), Content = Lorem.Sentence(12), NewsId = i, News = news }; _dbContext.Add <Comment>(comment); comments.Add(comment); } } news.Photo = photo; news.Comments = comments; _dbContext.Add <News>(news); _dbContext.Add <Photo>(photo); } AddNews("policy", _policyImgs, dateTime); AddNews("poland", _polandImgs, dateTime); AddNews("world", _worldImgs, dateTime); AddNews("business", _businessImgs, dateTime); AddNews("sport", _sportImgs, dateTime); _dbContext.SaveChanges(); } }