Проверка соответствия контакта и скважины при удалении, подправлены роуты

This commit is contained in:
Olga Nemt 2023-10-16 11:48:59 +05:00
parent c869dd705d
commit 494893257e
3 changed files with 14 additions and 11 deletions

View File

@ -55,9 +55,10 @@ namespace AsbCloudApp.Services
/// <summary>
/// Удаление контакта
/// </summary>
/// <param name="idWell">ключ скважины</param>
/// <param name="id">ключ скважины</param>
/// <param name="token"></param>
/// <returns></returns>
Task<int> DeleteAsync(int id, CancellationToken token);
Task<int> DeleteAsync(int idWell, int id, CancellationToken token);
}
}

View File

@ -70,22 +70,23 @@ namespace AsbCloudInfrastructure.Services
public async Task<int> UpdateAsync(ContactDto contactDto, CancellationToken token)
{
var dbContact = await GetContact(contactDto.IdWell, contactDto.Id, token);
if (dbContact is null)
throw new ForbidException("Contact doesn't exist");
var entity = contactDto.Adapt<Contact>();
db.Contacts.Update(entity);
return await db.SaveChangesAsync(token);
}
public async Task<int> DeleteAsync(int id, CancellationToken token)
public async Task<int> DeleteAsync(int idWell, int id, CancellationToken token)
{
var contact = await db.Contacts
.Where(c => c.Id == id)
.FirstOrDefaultAsync(token);
if (contact is null)
var dbContact = await GetContact(idWell, id, token);
if (dbContact is null)
throw new ForbidException("Contact doesn't exist");
db.Contacts.Remove(contact);
db.Contacts.Remove(dbContact);
return await db.SaveChangesAsync(token);
}
@ -94,6 +95,7 @@ namespace AsbCloudInfrastructure.Services
var contact = await db.Contacts
.Where(c => c.IdWell == idWell)
.Where(c => c.Id == idContact)
.AsNoTracking()
.FirstOrDefaultAsync(token);
return contact;

View File

@ -32,7 +32,7 @@ namespace AsbCloudWebApi.Controllers
/// </summary>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet("types")]
[HttpGet("type")]
[ProducesResponseType(typeof(IEnumerable<CompanyTypeDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetTypesAsync(CancellationToken token)
{
@ -47,7 +47,7 @@ namespace AsbCloudWebApi.Controllers
/// <param name="contactTypeId">тип контакта</param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet("contactType/{contactTypeId}")]
[HttpGet("type/{contactTypeId}")]
[ProducesResponseType(typeof(IEnumerable<ContactDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetAllAsync(int idWell, int contactTypeId, CancellationToken token)
{
@ -134,7 +134,7 @@ namespace AsbCloudWebApi.Controllers
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
var result = await wellContactsRepository.DeleteAsync(id, token);
var result = await wellContactsRepository.DeleteAsync(idWell, id, token);
return Ok(result);
}