public async Task<UserMessage> AddMessage(string userLogin, string text) { using (await _docLock.LockAsync().ConfigureAwait(false)) { var doc = await GetStorage(); var user = GetUserNode(doc, userLogin, true); var msg = new UserMessage() {UserLogin = userLogin, Text = text, Created = DateTime.UtcNow}; user.Add(Mapper.Map<XElement>(msg)); await SaveStorage(doc).ConfigureAwait(false); return msg; } }
public async Task<UserMessage> AddMessage(string userLogin, string text) { var res = new UserMessage() {Text = text, UserLogin = userLogin, Created = DateTime.UtcNow}; using (var conn = new SQLiteConnection(_connectionString)) { await conn.OpenAsync().ConfigureAwait(false); int count = await conn.ExecuteAsync( "insert or ignore into [Message] (Text, Created, UserId) select @text as Text, @created as Created, UserId from [User] where [Login] = @userLogin", new {text, created = res.Created, userLogin}).ConfigureAwait(false); if (count == 0) { int userId = await conn.ExecuteScalarAsync<int>( "insert into [User] (Login, Created) values(@userLogin, @created); SELECT last_insert_rowid() FROM [User]", new {userLogin, created = res.Created}).ConfigureAwait(false); await conn.ExecuteAsync("insert into [Message] (Text, Created, UserId) values(@text, @created, @userId)", new { text, created = res.Created, userId}).ConfigureAwait(false); } } return res; }