public static TagModel ToModel(Tag tagEntity) { TagModel tagModel = new TagModel() { ID = tagEntity.ID, Name = tagEntity.Name, PostsCount = tagEntity.Posts.Count }; return tagModel; }
public static Tag CreateOrLoadTag(string tagName, BloggingSystemContext context) { Tag exstingTag = context.Tags.FirstOrDefault<Tag>(t => t.Name == tagName); if (exstingTag != null) { return exstingTag; } Tag newTag = new Tag() { Name = tagName }; context.Tags.Add(newTag); context.SaveChanges(); return newTag; }
private static void GetTag(BlogContext context, string pt, List<Tag> result) { if (!string.IsNullOrEmpty(pt)) { var tag = context.Tags.FirstOrDefault(t => t.Name == pt); if (tag == null) { tag = new Tag() { Name = pt.ToLower() }; context.Tags.Add(tag); context.SaveChanges(); } result.Add(tag); } }
public HttpResponseMessage CreatePost(CreatePostDto value) { try { var sessionKey = ApiControllerHelper.GetHeaderValue(Request.Headers, "X-SessionKey"); if (sessionKey == null) { throw new ArgumentNullException("No session key provided in the request header!"); } Validate(value.Title, "title"); Validate(value.Text, "text"); var context = new BloggingSystemContext(); using (context) { var user = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey); if (user == null) { throw new ArgumentException("Users must be logged in to create posts!"); } var newPost = new Post() { Title = value.Title, Text = value.Text, PostDate = DateTime.Now, Author = user }; string[] tagsFromTitle = value.Title.Split( new char[] { ' ', ',', '.', ';', '!', '?', ':' }, StringSplitOptions.RemoveEmptyEntries); List<string> tagsToCheck = new List<string>(); foreach (var tagFromTitle in tagsFromTitle) { tagsToCheck.Add(tagFromTitle); } if (value.Tags != null) { foreach (string tagName in value.Tags) { tagsToCheck.Add(tagName); } } foreach (string tagName in tagsToCheck) { var matchingTag = context.Tags.FirstOrDefault(t => string.Compare(t.Name, tagName, true) == 0); if (matchingTag == null) { // tag not found, insert it in the database matchingTag = new Tag { Name = tagName.ToLower() }; context.Tags.Add(matchingTag); context.SaveChanges(); } newPost.Tags.Add(matchingTag); } context.Posts.Add(newPost); context.SaveChanges(); var createdPostDto = new CreatePostDto() { Id = newPost.Id, Title = newPost.Title, Tags = newPost.Tags.Select(t => t.Name), Text = newPost.Text }; var response = Request.CreateResponse(HttpStatusCode.Created, createdPostDto); return response; } } catch (Exception ex) { var errorResponse = Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message); throw new HttpResponseException(errorResponse); } }
private Tag GetOrCreateTag(BloggingSystemContext context, string tag) { var tagEntity = context.Tags.FirstOrDefault(t => t.Name == tag.ToLower()); if (tagEntity == null) { tagEntity = new Tag() { Name = tag.ToLower() }; } return tagEntity; }
public HttpResponseMessage PostPost(CreatePostModel model) { var responseMessage = this.PerformOperationAndHandleExceptions(() => { var sessionKey = this.GetHeaderValue(Request.Headers, "sessionKey"); var dbContext = new BlogContext(); // dbContext.Configuration.ProxyCreationEnabled = false; using (dbContext) { var user = dbContext.Users.FirstOrDefault(usr => usr.SessionKey == sessionKey); if (user == null) { throw new ArgumentException("Users must be logged when create a new post!"); } List<Tag> newTags = new List<Tag>(); if (!(model.Tags==null)) { foreach (var tagItem in model.Tags) { var tagEntity = new Tag() { Text = tagItem.ToLower() }; newTags.Add(tagEntity); } } //split title and add to the tags string[] wordsIntitle = model.Title.Split(new char[] { ' ', ',','.','!','?' }); foreach (var word in wordsIntitle) { if (!(word == string.Empty)) { var tagEntity = new Tag() { Text = word.ToLower().Trim() }; newTags.Add(tagEntity); } } var newPost = new Post() { Title = model.Title, Text = model.Text, PostDate = DateTime.Now, User = user, Tags=newTags }; dbContext.Posts.Add(newPost); dbContext.SaveChanges(); var postCreatedModel = new CreatedPostModel() { Title = newPost.Title, Id = newPost.Id }; var ret = Request.CreateResponse(HttpStatusCode.Created, postCreatedModel); // var ret = Request.CreateResponse(HttpStatusCode.Created); return ret; } }); return responseMessage; }
private static Tag CreateOrLoadTag(BlogContext context, string tagName) { Tag existingTag = (from t in context.Tags where t.Text == tagName select t).FirstOrDefault(); if (existingTag != null) { return existingTag; } Tag newTag = new Tag(); newTag.Text = tagName; context.Tags.Add(newTag); context.SaveChanges(); return newTag; }
public HttpResponseMessage PostCreate(PostModel postModel, [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey) { var responseMsg = this.ExceptionHandling( () => { var context = new BloggingSystemContext(); using (context) { var user = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey); if (user == null) { throw new InvalidOperationException("Invalid username or password!"); } List<string> tags = new List<string>(); tags = TakeTags(postModel.Title); foreach (var tagEntity in tags) { var tagInDb = context.Tags.FirstOrDefault(t => t.Name == tagEntity); if(tagInDb == null) { var tag = new Tag() { Name = tagEntity, User = user }; context.Tags.Add(tag); context.SaveChanges(); } } foreach (var tag in postModel.Tags) { var tagInDb = context.Tags.FirstOrDefault(t => t.Name == tag); if (tagInDb == null) { var newTag = new Tag() { Name = tag, User = user }; context.Tags.Add(newTag); context.SaveChanges(); } } var post = new Post() { Title = postModel.Title, Text = postModel.Text, User = user, PostDate = DateTime.Now }; context.Posts.Add(post); context.SaveChanges(); var postCreated = new PostCreatedModel() { Id = post.Id, Title = post.Title }; var response = this.Request.CreateResponse(HttpStatusCode.Created, postCreated); return response; } }); return responseMsg; }
public HttpResponseMessage PostAPost( [FromBody]PostModel model, [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey) { var responseMsg = this.PerformOperationAndHandleExceptions(() => { ValidateText(model.Text); ValidateTitle(model.Title); var context = new BloggingSystemContext(); using (context) { var user = GetValidUser(sessionKey); if (user == null) { throw new InvalidOperationException("You are not logged in!"); } var post = new Post() { Title = model.Title, Text = model.Text, PostDate = DateTime.Now, User = user }; HashSet<string> allTags = new HashSet<string>(); foreach (var tag in model.Tags) { var lowerTag = tag.ToLower(); if (!allTags.Contains(lowerTag)) { allTags.Add(lowerTag); } } string[] splitedTitle = model.Title.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); foreach (var tag in splitedTitle) { var lowerTag = tag.ToLower(); if (!allTags.Contains(lowerTag)) { allTags.Add(lowerTag); } } foreach (var tag in allTags) { var newTag = context.Tags.Where(t => t.Title == tag).FirstOrDefault(); if (newTag == null) { newTag = new Tag() { Title = tag }; } post.Tags.Add(newTag); } context.Posts.Add(post); context.SaveChanges(); PostResponseModel responseModel = new PostResponseModel() { Id = post.Id, Title = post.Title }; return Request.CreateResponse(HttpStatusCode.Created, responseModel); } }); return responseMsg; }