private void ConvertStoryVM(ref Story story, StoryVM storyVM) { storyVM.Slug = SlugUtil.GenerateSlug(storyVM.StoryName); story.StoryName = storyVM.StoryName; story.StoryProgress = storyVM.StoryProgress; story.StoryStatus = storyVM.StoryStatus; story.StoryDescription = storyVM.StoryDescription; story.AuthorId = storyVM.Author.AuthorId; story.Author = db.Authors.Find(storyVM.Author.AuthorId); story.CreatedDate = storyVM.CreatedDate; story.LastEditedDate = storyVM.LastEditedDate; story.UserId = storyVM.UserId; story.Score = storyVM.Score; story.RateCount = storyVM.RateCount; story.Image = storyVM.Image; story.Slug = storyVM.Slug; var slugCount = db.Stories.Count(s => s.Slug.StartsWith(storyVM.Slug)); if (slugCount > 0) { story.Slug += slugCount; } }
private void ConvertAuthorVM(ref Author author, AuthorVM authorVM) { authorVM.Slug = SlugUtil.GenerateSlug(authorVM.AuthorName); author.AuthorName = authorVM.AuthorName; author.AuthorStatus = authorVM.AuthorStatus; author.Slug = authorVM.Slug; var slugCount = db.Authors.Count(a => a.Slug.StartsWith(authorVM.Slug)); if (slugCount > 0) { author.Slug += slugCount; } }
private void ConvertGenreVM(ref Genre genre, GenreVM genreVM) { genreVM.Slug = SlugUtil.GenerateSlug(genreVM.GenreName); genre.GenreName = genreVM.GenreName; genre.GenreStatus = genreVM.GenreStatus; genre.Slug = genreVM.Slug; var slugCount = db.Genres.Count(g => g.Slug.StartsWith(genreVM.Slug)); if (slugCount > 0) { genre.Slug += slugCount; } }
internal override ArticleDAO CreateArticle(string title, string description, string body, string authorId) { var now = DateTime.Now; ArticleDAO article = new ArticleDAO { Slug = SlugUtil.CreateSlug(title), Title = title, Description = description, Body = body, AuthorId = authorId, CreatedAt = now, UpdatedAt = now }; _articles.Add(article); return(article); }
internal override void UpdateArticle(int articleId, UpdateArticle articleUpdates) { ArticleDAO article = _articles.Where(e => e.Id == articleId).Single(); if (null != articleUpdates.Title) { article.Title = articleUpdates.Title; article.Slug = SlugUtil.CreateSlug(articleUpdates.Title); } if (null != articleUpdates.Description) { article.Description = articleUpdates.Description; } if (null != articleUpdates.Body) { article.Body = articleUpdates.Body; } article.UpdatedAt = DateTime.Now; _articles.Update(article); }
public IHttpActionResult PutReview(int id, Review review) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != review.ReviewId) { return(BadRequest()); } review.Slug = SlugUtil.GenerateSlug(review.ReviewTitle); var slugCount = db.Reviews.Count(r => r.Slug.StartsWith(review.Slug)); if (slugCount > 0) { review.Slug += slugCount; } db.Entry(review).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!ReviewExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.OK)); }
public IHttpActionResult PostReview(Review review) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } review.Slug = SlugUtil.GenerateSlug(review.ReviewTitle); var slugCount = db.Reviews.Count(r => r.Slug.StartsWith(review.Slug)); if (slugCount > 0) { review.Slug += slugCount; } db.Reviews.Add(review); db.SaveChanges(); return(CreatedAtRoute("NameApi", new { slug = review.Slug }, review)); }
private void ConvertChapterVM(ref Chapter chapter, ChapterVM chapterVM) { chapterVM.Slug = SlugUtil.GenerateSlug(chapterVM.ChapterTitle); chapter.StoryId = chapterVM.StoryId; chapter.ChapterNumber = chapterVM.ChapterNumber; chapter.ChapterTitle = chapterVM.ChapterTitle; chapter.ChapterContent = chapterVM.ChapterContent; chapter.ChapterStatus = chapterVM.ChapterStatus; chapter.UploadedDate = chapterVM.UploadedDate; chapter.LastEditedDate = chapterVM.LastEditedDate; chapter.UserId = chapterVM.UserId; chapter.Slug = chapterVM.Slug; var slugCount = db.Chapters.Count(c => c.Slug.StartsWith(chapterVM.Slug)); if (slugCount > 0) { chapter.Slug += slugCount; } }
public async Task <IActionResult> createArticle([FromBody] ArticleHTTPTransferObject articleHTTPTransferObject) { var authedUser = HttpContext.User; if (!authedUser.HasClaim(c => c.Type == ClaimTypes.NameIdentifier)) { this.HttpContext.Response.StatusCode = 401; var authErrorResponse = new ErrorResponse(); authErrorResponse.addErrorKey("Missing authentication"); return(Json(authErrorResponse)); } string authUsername = authedUser.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value; ApplicationUser authedUserInDB = await _userManager.FindByNameAsync(authUsername); if (null == authedUserInDB) { this.HttpContext.Response.StatusCode = 404; var authErrorResponse = new ErrorResponse(); authErrorResponse.addErrorKey("Authenticated user not in database"); return(Json(authErrorResponse)); } Article article = articleHTTPTransferObject.Article; if (null == article.Title || null == article.Description || null == article.Body) { this.HttpContext.Response.StatusCode = 422; var authErrorResponse = new ErrorResponse(); authErrorResponse.addErrorKey("Article requires a title, description, and body"); return(Json(authErrorResponse)); } try { article.Slug = SlugUtil.GenerateSlug(article.Title); article.AuthorId = authedUserInDB.Id; article.CreatedAt = DateTime.Now; article.UpdatedAt = DateTime.Now; _context.Articles.Add(article); _context.SaveChanges(); foreach (string tag in article.TagList) { Tag tagEntity; if (!_context.Tags.Any(e => e.TagName == tag)) { tagEntity = new Tag(); tagEntity.TagName = tag; _context.Tags.Add(tagEntity); } else { tagEntity = _context.Tags.FirstOrDefault(e => e.TagName == tag); } ArticleTags articleTags = new ArticleTags(); articleTags.ArticleId = article.Id; articleTags.TagId = tagEntity.TagId; _context.ArticleTags.Add(articleTags); _context.SaveChanges(); } } catch (DbUpdateException ex) { this.HttpContext.Response.StatusCode = 422; var authErrorResponse = new ErrorResponse(); authErrorResponse.addErrorKey(ex.Message); return(Json(authErrorResponse)); } this.HttpContext.Response.StatusCode = 201; articleHTTPTransferObject = new ArticleHTTPTransferObject(); articleHTTPTransferObject.Article = article; return(Json(article)); }