diff --git a/AsbCloudApp/Services/IWellContactService.cs b/AsbCloudApp/Services/IWellContactService.cs
index 4868e3b2..87239cb4 100644
--- a/AsbCloudApp/Services/IWellContactService.cs
+++ b/AsbCloudApp/Services/IWellContactService.cs
@@ -60,5 +60,7 @@ namespace AsbCloudApp.Services
///
///
Task DeleteAsync(int idWell, int id, CancellationToken token);
+
+ Task CopyAsync(int idWell, int idWellTarget, IEnumerable contactIds, CancellationToken token);
}
}
diff --git a/AsbCloudInfrastructure/Services/WellContactService.cs b/AsbCloudInfrastructure/Services/WellContactService.cs
index d4f81568..1d83799b 100644
--- a/AsbCloudInfrastructure/Services/WellContactService.cs
+++ b/AsbCloudInfrastructure/Services/WellContactService.cs
@@ -34,6 +34,19 @@ namespace AsbCloudInfrastructure.Services
return entities;
}
+ public async Task> GetAllAsync(int wellId, IEnumerable contactIds, CancellationToken token)
+ {
+ var query = db.Contacts
+ .Where(c => c.IdWell == wellId)
+ .Where(c => contactIds.Contains(c.Id))
+ .Select(c => c.Adapt());
+
+ var entities = await query.AsNoTracking()
+ .ToArrayAsync(token);
+
+ return entities;
+ }
+
public async Task GetAsync(int idWell, int id, CancellationToken token)
{
var dbContact = await GetContact(idWell, id, token);
@@ -90,6 +103,20 @@ namespace AsbCloudInfrastructure.Services
return await db.SaveChangesAsync(token);
}
+ public async Task CopyAsync(int idWell, int idWellTarget, IEnumerable contactIds, CancellationToken token)
+ {
+ var dbContacts = await GetContacts(idWell, contactIds, token);
+
+ foreach (var dbContact in dbContacts)
+ {
+ dbContact.IdWell = idWellTarget;
+ }
+
+ await db.Contacts.AddRangeAsync(dbContacts);
+
+ return await db.SaveChangesAsync(token);
+ }
+
private async Task GetContact(int idWell, int idContact, CancellationToken token)
{
var contact = await db.Contacts
@@ -100,5 +127,16 @@ namespace AsbCloudInfrastructure.Services
return contact;
}
+
+ private async Task GetContacts(int idWell, IEnumerable contactIds, CancellationToken token)
+ {
+ var contacts = await db.Contacts
+ .Where(c => c.IdWell == idWell)
+ .Where(c => contactIds.Contains(c.Id))
+ .AsNoTracking()
+ .ToArrayAsync(token);
+
+ return contacts;
+ }
}
}
diff --git a/AsbCloudWebApi/Controllers/WellContactController.cs b/AsbCloudWebApi/Controllers/WellContactController.cs
index a63ec0d9..089355b9 100644
--- a/AsbCloudWebApi/Controllers/WellContactController.cs
+++ b/AsbCloudWebApi/Controllers/WellContactController.cs
@@ -139,6 +139,34 @@ namespace AsbCloudWebApi.Controllers
return Ok(result);
}
+ ///
+ /// Скопировать контакты в другую скважину
+ ///
+ /// ключ скважины, откуда копировать контакты
+ /// ключ скважины, куда копировать контакты
+ ///
+ ///
+ ///
+ [HttpPost("copy/{id}")]
+ [Permission]
+ [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
+ public async Task CopyAsync(
+ int idWell,
+ int idWellTarget,
+ [FromBody] IEnumerable contactIds,
+ CancellationToken token)
+ {
+ if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
+ return Forbid();
+
+ if (!await CanUserAccessToWellAsync(idWellTarget, token).ConfigureAwait(false))
+ return Forbid();
+
+ var result = await wellContactsRepository.CopyAsync(idWell, idWellTarget, contactIds, token);
+
+ return Ok(result);
+ }
+
private async Task CanUserAccessToWellAsync(int idWell, CancellationToken token)
{
int? idCompany = User.GetCompanyId();