forked from ddrilling/AsbCloudServer
Перенос контактов из одной скважины на другую. Начало
This commit is contained in:
parent
1293194fee
commit
a9d365908b
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
|
Loading…
Reference in New Issue
Block a user