Merge pull request 'Контакты/Добавить метод для переноса нескольких контактов' (#294) from feature/#36049589-copy-contacts-from-well-to-well into dev

Reviewed-on: https://test.digitaldrilling.ru:8443/DDrilling/AsbCloudServer/pulls/294
This commit is contained in:
Никита Фролов 2024-07-26 17:04:39 +05:00
commit 2f5dfaae36
3 changed files with 69 additions and 0 deletions

View File

@ -60,5 +60,15 @@ namespace AsbCloudApp.Services
/// <param name="token"></param>
/// <returns></returns>
Task<int> DeleteAsync(int idWell, int id, CancellationToken token);
/// <summary>
/// Скопировать контакты в другую скважину
/// </summary>
/// <param name="idWell">ключ скважины, откуда копировать контакты</param>
/// <param name="idWellTarget">ключ скважины, куда копировать контакты</param>
/// <param name="contactIds"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<int> CopyAsync(int idWell, int idWellTarget, IEnumerable<int> contactIds, CancellationToken token);
}
}

View File

@ -90,6 +90,26 @@ namespace AsbCloudInfrastructure.Services
return await db.SaveChangesAsync(token);
}
public async Task<int> CopyAsync(int idWell, int idWellTarget, IEnumerable<int> contactIds, CancellationToken token)
{
var contacts = await GetContacts(idWell, contactIds, token);
if (!contacts.Any())
return 0;
var newContacts = contacts.Select(contact =>
{
var newContact = contact.Adapt<Contact>();
newContact.IdWell = idWellTarget;
newContact.Id = default;
return newContact;
});
db.Contacts.AddRange(newContacts);
return await db.SaveChangesAsync(token);
}
private async Task<Contact?> GetContact(int idWell, int idContact, CancellationToken token)
{
var contact = await db.Contacts
@ -100,5 +120,16 @@ namespace AsbCloudInfrastructure.Services
return contact;
}
private async Task<Contact[]> GetContacts(int idWell, IEnumerable<int> contactIds, CancellationToken token)
{
var contacts = await db.Contacts
.Where(c => c.IdWell == idWell)
.Where(c => contactIds.Contains(c.Id))
.AsNoTracking()
.ToArrayAsync(token);
return contacts;
}
}
}

View File

@ -139,6 +139,34 @@ namespace AsbCloudWebApi.Controllers
return Ok(result);
}
/// <summary>
/// Скопировать контакты в другую скважину
/// </summary>
/// <param name="idWell">ключ скважины, откуда копировать контакты</param>
/// <param name="idWellTarget">ключ скважины, куда копировать контакты</param>
/// <param name="contactIds"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpPost("copy/{idWellTarget}")]
[Permission]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> CopyAsync(
int idWell,
int idWellTarget,
[FromBody] IEnumerable<int> 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<bool> CanUserAccessToWellAsync(int idWell, CancellationToken token)
{
int? idCompany = User.GetCompanyId();