Перенос контактов из одной скважины на другую. Начало

This commit is contained in:
Olga Nemt 2024-07-25 14:55:05 +05:00
parent 1293194fee
commit a9d365908b
3 changed files with 68 additions and 0 deletions

View File

@ -60,5 +60,7 @@ namespace AsbCloudApp.Services
/// <param name="token"></param>
/// <returns></returns>
Task<int> DeleteAsync(int idWell, int id, CancellationToken token);
Task<int> CopyAsync(int idWell, int idWellTarget, IEnumerable<int> contactIds, CancellationToken token);
}
}

View File

@ -34,6 +34,19 @@ namespace AsbCloudInfrastructure.Services
return entities;
}
public async Task<IEnumerable<ContactDto>> GetAllAsync(int wellId, IEnumerable<int> contactIds, CancellationToken token)
{
var query = db.Contacts
.Where(c => c.IdWell == wellId)
.Where(c => contactIds.Contains(c.Id))
.Select(c => c.Adapt<ContactDto>());
var entities = await query.AsNoTracking()
.ToArrayAsync(token);
return entities;
}
public async Task<ContactDto?> 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<int> CopyAsync(int idWell, int idWellTarget, IEnumerable<int> 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<Contact?> GetContact(int idWell, int idContact, CancellationToken token)
{
var contact = await db.Contacts
@ -100,5 +127,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/{id}")]
[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();