public static TagModel TagToTagModel(Tag tag) { var tagModel = new TagModel() { TagId = tag.TagId, Name = tag.Name, Posts = tag.Posts == null ? 0 : tag.Posts.Count }; return tagModel; }
public void AddPost() { Random rnd = new Random(); if (this.view.AuthorId == null) { throw new ArgumentException("User have to be logged in to add new post"); } if (this.view.PostTitle.IsNullOrWhiteSpace()) { throw new ArgumentException("Title cannot be null or whitespace"); } if (this.view.Content.IsNullOrWhiteSpace()) { throw new ArgumentException("Content cannot be null or whitespace"); } var category = this.Data.Categories.All().FirstOrDefault(c => c.Name == this.view.Category); if (category == null) { throw new ArgumentException("Not existing Category"); } List<string> tagsNames = this.view.Tags.Split(',').Select(p => p.Trim()).ToList(); List<Tag> tags = new List<Tag>(); if (tagsNames.Any(t => t != string.Empty)) { tags = this.Data.Tags.All().Where(t => tagsNames.Contains(t.Name)).ToList(); foreach (var tagName in tagsNames) { if (!tags.Any(t => t.Name == tagName)) { var tag = new Tag { Name = tagName, Slug = this.CreateSlug(tagName) }; this.Data.Tags.Add(tag); } } this.Data.SaveChanges(); tags = this.Data.Tags.All().Where(t => tagsNames.Contains(t.Name)).ToList(); } var post = new Post { Title = this.view.PostTitle, Slug = this.CreateSlug( string.Format("{0}-{1}", this.view.PostTitle, rnd.Next(10000,100000))), Content = this.view.Content, CategoryId = category.Id, AuthorId = this.view.AuthorId, DateCreated = DateTime.Now, Tags = tags }; this.Data.Posts.Add(post); this.Data.SaveChanges(); }
private HashSet<Tag> GetOrCreateTags(IEnumerable<TagUploadModel> uploadedTags) { var tags = new HashSet<Tag>(); foreach (var uploadedTag in uploadedTags) { var tag = this.data.Tags.All().FirstOrDefault(t => t.Name == uploadedTag.Name); if (tag == null) { tag = new Tag() { Name = uploadedTag.Name }; this.data.Tags.Add(tag); this.data.SaveChanges(); } tags.Add(tag); } return tags; }
public HttpResponseMessage PostCreatePost(PostModel model, [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey) { var responseMsg = this.PerformOperationAndHandleExceptions(() => { this.ValidateSessionKey(sessionKey); if (model == null) { throw new InvalidOperationException("Model cannot be null"); } var context = new BlogSystemContext(); using (context) { User user = context.Users.Where(usr => usr.SessionKey == sessionKey).FirstOrDefault(); if (user == null) { throw new InvalidOperationException("Invalid sessionkey"); } var titleTags = model.Title.Split( new char[] { '.', ',', ';', '!', '?', ' ' }, StringSplitOptions.RemoveEmptyEntries); var newTags = new HashSet<Tag>(); foreach (var tagName in titleTags) { Tag newtag = new Tag() { Name = tagName.ToLower() }; if (!newTags.Contains(newtag)) { newTags.Add(newtag); } } foreach (var tagName in model.Tags) { Tag newtag = new Tag() { Name = tagName.ToLower() }; if (!newTags.Contains(newtag)) { newTags.Add(newtag); } } var newDbTags = new HashSet<Tag>(); foreach (var tag in newTags) { var tagDb = context.Tags.FirstOrDefault(tg => tg.Name == tag.Name); if (tagDb == null) { newDbTags.Add(tag); } else { newDbTags.Add(tagDb); } } var post = new Post() { Title = model.Title, Text = model.Text, DatePosted = DateTime.Now, User = user, Tags = newDbTags }; user.Posts.Add(post); context.SaveChanges(); var responseModel = new CreatePostResponseModel() { Title = post.Title, Id = post.Id }; var response = this.Request.CreateResponse(HttpStatusCode.Created, responseModel); return response; } }); return responseMsg; }
public HttpResponseMessage PostPost(CreatePostModel model, [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey) { var responseMsg = ExceptionHandler( () => { User user; var context = new BlogSystemContext(); using (context) { user = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey); if (user == null) { throw new InvalidOperationException("Invalid username or password"); } } Dictionary<string, Tag> tags = new Dictionary<string, Tag>(); foreach (var tag in model.Tags) { var tagToLower = tag.ToLower(); if (!tags.ContainsKey(tagToLower)) { Tag newTag = new Tag { Name = tagToLower }; tags.Add(tagToLower, newTag); } } var titleTags = model.Title.Split(' '); foreach (var tag in titleTags) { var tagToLower = tag.ToLower(); if (!tags.ContainsKey(tagToLower)) { Tag newTag = new Tag { Name = tagToLower }; tags.Add(tagToLower, newTag); } } List<Tag> filtTags = new List<Tag>(); foreach (var tag in tags.Values) { filtTags.Add(tag); } var newPost = new Post { Title = model.Title, Text = model.Text, Tags = filtTags, PostDate = DateTime.Now, User = user, }; var dbPost = this.postRepository.Add(newPost); var postMode = new CreatedPostModel { Id = dbPost.Id, Title = dbPost.Title }; var response = this.Request.CreateResponse(HttpStatusCode.Created, postMode); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = postMode.Id })); return response; }); return responseMsg; }
public HttpResponseMessage PostNewPost(PostNewModel model, [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey) { var responseMsg = this.PerformOperationAndHandleExceptions( () => { var context = new BlogSystemContext(); using (context) { var user = context.Users.FirstOrDefault( usr => usr.SessionKey == sessionKey); if (user == null) { throw new InvalidOperationException("Invalid sessionKey"); } if (model.Title == null || model.Text == null) { throw new ArgumentNullException("Post title or post text cannot be null"); } string[] titleWords = model.Title.Split( new char[] { ' ', ',', '.', '!', '?', '\'', '(', ')' }, StringSplitOptions.RemoveEmptyEntries); IList<Tag> tags = new List<Tag>(); if (model.Tags != null) { foreach (var item in model.Tags) { var tag = context.Tags.FirstOrDefault(t => t.Name == item.ToLower()); if (tag == null) { tag = new Tag() { Name = item.ToLower() }; context.Tags.Add(tag); context.SaveChanges(); } tags.Add(tag); } } foreach (var item in titleWords) { var tag = context.Tags.FirstOrDefault(t => t.Name == item.ToLower()); if (tag == null) { tag = new Tag() { Name = item.ToLower() }; context.Tags.Add(tag); context.SaveChanges(); } tags.Add(tag); } var post = new Post() { Title = model.Title, Text = model.Text, PostDate = DateTime.Now, User = user, Tags = tags }; context.Posts.Add(post); context.SaveChanges(); var createdModel = new PostCreatedModel() { Id = post.Id, Title = post.Title }; var response = this.Request.CreateResponse(HttpStatusCode.Created, createdModel); return response; } }); return responseMsg; }