Created
December 12, 2014 16:38
-
-
Save AlexArchive/b9fb33a19784d8c2350f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AddPostCommandHandler : IRequestHandler<AddPostCommand, string> | |
{ | |
public string Handle(AddPostCommand command) | |
{ | |
var param = new | |
{ | |
PublishDate = DateTime.Now, | |
Slug = SlugConverter.Convert(command.Title), | |
command.Title, | |
command.Body, | |
command.Published | |
}; | |
using (var connection = SqlConnectionFactory.Create()) | |
{ | |
if (SlugTaken(connection, param.Slug)) | |
{ | |
return null; | |
} | |
connection.Execute( | |
"INSERT INTO [Posts] VALUES (@Slug, @Title, @Body, @Published, @PublishDate)", | |
param); | |
return param.Slug; | |
} | |
} | |
private static bool SlugTaken(IDbConnection connection, string slug) | |
{ | |
var param = new { Slug = slug }; | |
var record = connection.ExecuteScalar( | |
"SELECT TOP 1 [Slug] FROM [Posts] WHERE [Slug] = @Slug", | |
param); | |
return record != null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment