|
@@ -1,5 +1,6 @@
|
|
|
using Dapper;
|
|
|
using System.Collections.Generic;
|
|
|
+using System.Linq;
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
namespace Robware.Mailboxes.Data {
|
|
@@ -15,5 +16,39 @@ namespace Robware.Mailboxes.Data {
|
|
|
var result = await connection.QueryAsync<Alias>(query);
|
|
|
return result;
|
|
|
}
|
|
|
+
|
|
|
+ private async Task<bool> SetActive(int id, int active)
|
|
|
+ {
|
|
|
+ const string query = "UPDATE virtual_aliases SET active = @active WHERE id = @id;";
|
|
|
+ try {
|
|
|
+ using var connection = _dbProvider.NewConnection();
|
|
|
+ connection.Open();
|
|
|
+ await connection.ExecuteAsync(query, new {active, id});
|
|
|
+ }
|
|
|
+ catch {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ public async Task<bool> Enable(int id) => await SetActive(id, 1);
|
|
|
+
|
|
|
+ public async Task<bool> Disable(int id) => await SetActive(id, 0);
|
|
|
+
|
|
|
+ public async Task<int> Create(string source, string destination, bool active) {
|
|
|
+ const string query = "INSERT INTO virtual_aliases (domain_id, source, destination, active) VALUES (1, @source, @destination, @active); SELECT CAST(LAST_INSERT_ID() as int)";
|
|
|
+ using var connection = _dbProvider.NewConnection();
|
|
|
+ connection.Open();
|
|
|
+ var result = await connection.QueryAsync<int>(query, new {source, destination, active = active ? 1 : 0});
|
|
|
+ return result.Single();
|
|
|
+ }
|
|
|
+
|
|
|
+ public async Task<bool> Delete(int id) {
|
|
|
+ const string query = "DELETE FROM virtual_aliases WHERE id = @id;";
|
|
|
+ using var connection = _dbProvider.NewConnection();
|
|
|
+ connection.Open();
|
|
|
+ await connection.ExecuteAsync(query, new {id});
|
|
|
+ return true;
|
|
|
+ }
|
|
|
}
|
|
|
}
|