Skip to content

Commit

Permalink
prevent duplicate titles
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Apr 3, 2024
1 parent df6aab8 commit f3e8755
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions MyApp.ServiceInterface/QuestionServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,28 @@ public async Task<object> Any(AskQuestion request)

var userName = GetUserName();
var now = DateTime.UtcNow;
var postId = (int)appConfig.GetNextPostId();
var postId = (int)appConfig.GetNextPostId();
var title = request.Title.Trim();
var body = request.Body.Trim();
var slug = request.Title.GenerateSlug(200);
var summary = request.Body.StripHtml().SubstringWithEllipsis(0, 200);

var existingPost = await Db.SingleAsync(Db.From<Post>().Where(x => x.Title == title));
if (existingPost != null)
throw new ArgumentException($"Question with title '{title}' already used in question {existingPost.Id}", nameof(Post.Title));

Post createPost() => new()
{
Id = postId,
PostTypeId = 1,
Title = request.Title,
Title = title,
Tags = tags,
Slug = slug,
Summary = summary,
CreationDate = now,
CreatedBy = userName,
LastActivityDate = now,
Body = request.Body,
Body = body,
RefId = request.RefId,
};

Expand Down

0 comments on commit f3e8755

Please sign in to comment.