From a9d365908b9120995cf9c0edcd8cb3f46a91e64c Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Thu, 25 Jul 2024 14:55:05 +0500 Subject: [PATCH 1/5] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=BD=D0=BE=D1=81?= =?UTF-8?q?=20=D0=BA=D0=BE=D0=BD=D1=82=D0=B0=D0=BA=D1=82=D0=BE=D0=B2=20?= =?UTF-8?q?=D0=B8=D0=B7=20=D0=BE=D0=B4=D0=BD=D0=BE=D0=B9=20=D1=81=D0=BA?= =?UTF-8?q?=D0=B2=D0=B0=D0=B6=D0=B8=D0=BD=D1=8B=20=D0=BD=D0=B0=20=D0=B4?= =?UTF-8?q?=D1=80=D1=83=D0=B3=D1=83=D1=8E.=20=D0=9D=D0=B0=D1=87=D0=B0?= =?UTF-8?q?=D0=BB=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(); From dad0480f5942f0b5e297b2bf78c971176921ef9f Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Thu, 25 Jul 2024 15:38:14 +0500 Subject: [PATCH 2/5] =?UTF-8?q?=D0=94=D0=BF=D0=B8=D1=81=D0=B0=D0=BD=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=BC=D0=BC=D0=B5=D0=BD=D1=82=D0=B0=D1=80=D0=B8?= =?UTF-8?q?=D0=B9=20=D0=BA=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D1=83,=20?= =?UTF-8?q?=D1=83=D0=B1=D1=80=D0=B0=D0=BD=20=D0=BB=D0=B8=D1=88=D0=BD=D0=B8?= =?UTF-8?q?=D0=B9=20=D0=BA=D0=BE=D0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AsbCloudApp/Services/IWellContactService.cs | 8 ++++++++ .../Services/WellContactService.cs | 14 +------------- .../Controllers/WellContactController.cs | 2 +- 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/AsbCloudApp/Services/IWellContactService.cs b/AsbCloudApp/Services/IWellContactService.cs index 87239cb4..a584fad7 100644 --- a/AsbCloudApp/Services/IWellContactService.cs +++ b/AsbCloudApp/Services/IWellContactService.cs @@ -61,6 +61,14 @@ 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 1d83799b..2fbd9be0 100644 --- a/AsbCloudInfrastructure/Services/WellContactService.cs +++ b/AsbCloudInfrastructure/Services/WellContactService.cs @@ -34,19 +34,6 @@ 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); @@ -110,6 +97,7 @@ namespace AsbCloudInfrastructure.Services foreach (var dbContact in dbContacts) { dbContact.IdWell = idWellTarget; + dbContact.Id = default; } await db.Contacts.AddRangeAsync(dbContacts); diff --git a/AsbCloudWebApi/Controllers/WellContactController.cs b/AsbCloudWebApi/Controllers/WellContactController.cs index 089355b9..bdb6a7ae 100644 --- a/AsbCloudWebApi/Controllers/WellContactController.cs +++ b/AsbCloudWebApi/Controllers/WellContactController.cs @@ -147,7 +147,7 @@ namespace AsbCloudWebApi.Controllers /// /// /// - [HttpPost("copy/{id}")] + [HttpPost("copy/{idWellTarget}")] [Permission] [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] public async Task CopyAsync( From ac8be4f3db0b8119986ea9bca91e22244026d1b4 Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Thu, 25 Jul 2024 15:46:42 +0500 Subject: [PATCH 3/5] =?UTF-8?q?=D0=A1=D0=BE=D0=BE=D0=B1=D1=89=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BE=D0=B1=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA?= =?UTF-8?q?=D0=B5,=20=D0=B5=D1=81=D0=BB=D0=B8=20=D0=BA=D0=BE=D0=BD=D1=82?= =?UTF-8?q?=D0=B0=D0=BA=D1=82=D1=8B=20=D0=B4=D0=BB=D1=8F=20=D0=BF=D0=B5?= =?UTF-8?q?=D1=80=D0=B5=D0=BD=D0=BE=D1=81=D0=B0=20=D0=BD=D0=B5=20=D0=BD?= =?UTF-8?q?=D0=B0=D0=B9=D0=B4=D0=B5=D0=BD=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AsbCloudInfrastructure/Services/WellContactService.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/AsbCloudInfrastructure/Services/WellContactService.cs b/AsbCloudInfrastructure/Services/WellContactService.cs index 2fbd9be0..525ce6f9 100644 --- a/AsbCloudInfrastructure/Services/WellContactService.cs +++ b/AsbCloudInfrastructure/Services/WellContactService.cs @@ -93,6 +93,8 @@ namespace AsbCloudInfrastructure.Services public async Task CopyAsync(int idWell, int idWellTarget, IEnumerable contactIds, CancellationToken token) { var dbContacts = await GetContacts(idWell, contactIds, token); + if (!dbContacts.Any()) + throw new ForbidException("contacts not found"); foreach (var dbContact in dbContacts) { From 875498a4a6f68a32b03f1e3d20c6e6719ffc1b7b Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Fri, 26 Jul 2024 10:18:00 +0500 Subject: [PATCH 4/5] =?UTF-8?q?=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=BD=D0=B0=D0=B7=D0=B2=D0=B0=D0=BD=D0=B8=D1=8F=20=D0=BF=D0=B5?= =?UTF-8?q?=D1=80=D0=B5=D0=BC=D0=B5=D0=BD=D0=BD=D0=BE=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/WellContactService.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/AsbCloudInfrastructure/Services/WellContactService.cs b/AsbCloudInfrastructure/Services/WellContactService.cs index 525ce6f9..7e628e9e 100644 --- a/AsbCloudInfrastructure/Services/WellContactService.cs +++ b/AsbCloudInfrastructure/Services/WellContactService.cs @@ -92,17 +92,17 @@ namespace AsbCloudInfrastructure.Services public async Task CopyAsync(int idWell, int idWellTarget, IEnumerable contactIds, CancellationToken token) { - var dbContacts = await GetContacts(idWell, contactIds, token); - if (!dbContacts.Any()) + var newContacts = await GetContacts(idWell, contactIds, token); + if (!newContacts.Any()) throw new ForbidException("contacts not found"); - foreach (var dbContact in dbContacts) + foreach (var newContact in newContacts) { - dbContact.IdWell = idWellTarget; - dbContact.Id = default; + newContact.IdWell = idWellTarget; + newContact.Id = default; } - await db.Contacts.AddRangeAsync(dbContacts); + await db.Contacts.AddRangeAsync(newContacts); return await db.SaveChangesAsync(token); } From e525e05584fe3af5aea6680a76179d7c006f91a0 Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Fri, 26 Jul 2024 16:19:24 +0500 Subject: [PATCH 5/5] =?UTF-8?q?=D0=9F=D1=80=D0=B0=D0=B2=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=BF=D0=BE=20=D1=80=D0=B5=D0=B7=D1=83=D0=BB=D1=8C=D1=82=D0=B0?= =?UTF-8?q?=D1=82=D0=B0=20=D1=80=D0=B5=D0=B2=D1=8C=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AsbCloudApp/Services/IWellContactService.cs | 8 ++++---- .../Services/WellContactService.cs | 15 +++++++++------ 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/AsbCloudApp/Services/IWellContactService.cs b/AsbCloudApp/Services/IWellContactService.cs index a584fad7..266da573 100644 --- a/AsbCloudApp/Services/IWellContactService.cs +++ b/AsbCloudApp/Services/IWellContactService.cs @@ -62,11 +62,11 @@ 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 7e628e9e..1bb45b77 100644 --- a/AsbCloudInfrastructure/Services/WellContactService.cs +++ b/AsbCloudInfrastructure/Services/WellContactService.cs @@ -92,17 +92,20 @@ namespace AsbCloudInfrastructure.Services public async Task CopyAsync(int idWell, int idWellTarget, IEnumerable contactIds, CancellationToken token) { - var newContacts = await GetContacts(idWell, contactIds, token); - if (!newContacts.Any()) - throw new ForbidException("contacts not found"); + var contacts = await GetContacts(idWell, contactIds, token); + if (!contacts.Any()) + return 0; - foreach (var newContact in newContacts) + var newContacts = contacts.Select(contact => { + var newContact = contact.Adapt(); newContact.IdWell = idWellTarget; newContact.Id = default; - } - await db.Contacts.AddRangeAsync(newContacts); + return newContact; + }); + + db.Contacts.AddRange(newContacts); return await db.SaveChangesAsync(token); }