From a9d365908b9120995cf9c0edcd8cb3f46a91e64c Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Thu, 25 Jul 2024 14:55:05 +0500 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=BD=D0=BE=D1=81=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=BD=D1=82=D0=B0=D0=BA=D1=82=D0=BE=D0=B2=20=D0=B8?= =?UTF-8?q?=D0=B7=20=D0=BE=D0=B4=D0=BD=D0=BE=D0=B9=20=D1=81=D0=BA=D0=B2?= =?UTF-8?q?=D0=B0=D0=B6=D0=B8=D0=BD=D1=8B=20=D0=BD=D0=B0=20=D0=B4=D1=80?= =?UTF-8?q?=D1=83=D0=B3=D1=83=D1=8E.=20=D0=9D=D0=B0=D1=87=D0=B0=D0=BB?= =?UTF-8?q?=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AsbCloudApp/Services/IWellContactService.cs | 2 + .../Services/WellContactService.cs | 38 +++++++++++++++++++ .../Controllers/WellContactController.cs | 28 ++++++++++++++ 3 files changed, 68 insertions(+) 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();