-
-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathCommand.cs
30 lines (25 loc) · 1.02 KB
/
Command.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using System.Data;
using Dapper;
using HappyCode.NetCoreBoilerplate.BooksModule.Dtos;
namespace HappyCode.NetCoreBoilerplate.BooksModule.Features.UpsertBook;
internal static class Command
{
private static readonly string _upsertBook = @$"
INSERT INTO Books ({nameof(BookDto.Id)}, {nameof(BookDto.Title)})
VALUES(@id, @title)
ON CONFLICT ({nameof(BookDto.Id)}) DO UPDATE SET {nameof(BookDto.Title)} = excluded.{nameof(BookDto.Title)}
";
private static readonly string _nextId = @$"
SELECT IFNULL(MAX({nameof(BookDto.Id)}), 0) + 1
FROM Books
";
public static async Task UpsertBookAsync(this IDbConnection db, BookDto book, CancellationToken cancellationToken)
{
var id = book.Id ?? await db.ExecuteScalarAsync<int>(new CommandDefinition(_nextId, cancellationToken: cancellationToken));
await db.ExecuteAsync(new CommandDefinition(_upsertBook, new
{
id = id,
title = book.Title,
}, cancellationToken: cancellationToken));
}
}