daminko
an error occured while sending an update request
This my method :
public async Task<IActionResult> Update([FromRoute] string matricule, [FromBody] UpdateUserRequestDTO updateDto)
{
var user = await _utilisateurService.GetByMatriculeAsync(matricule);
if (user == null)
{
return NotFound();
}
if (user.Data.Role == "Transporteur")
{
var transporteur = await _transporteurService.GetByMatriculeAsync(user.Data);
if (transporteur == null)
{
return NotFound("Transporteur not found");
}
if (transporteur.Data!.VehiculeId != updateDto.VehiculeId)
{
var switchNew = await _vehiculeService.SwitchLibre(updateDto.VehiculeId!);
var switchOld = await _vehiculeService.SwitchLibre(transporteur.Data.VehiculeId!);
if (!switchNew.Success || !switchOld.Success)
{
// Roll back the switch operation if either fails
if (!switchNew.Success)
{
await _vehiculeService.SwitchLibre(transporteur.Data.VehiculeId!);
}
if (!switchOld.Success)
{
await _vehiculeService.SwitchLibre(updateDto.VehiculeId!);
}
return BadRequest("Failed to switch vehicle availability");
}
}
var updatedTransporteur = await _transporteurService.UpdateTransporteur(updateDto, matricule);
return updatedTransporteur.Success ? Ok(updatedTransporteur.Data) : BadRequest(updatedTransporteur.Message);
}
else
{
return BadRequest("Invalid role");
}
}
public async Task<IActionResult> Update([FromRoute] string matricule, [FromBody] UpdateUserRequestDTO updateDto)
{
var user = await _utilisateurService.GetByMatriculeAsync(matricule);
if (user == null)
{
return NotFound();
}
if (user.Data.Role == "Transporteur")
{
var transporteur = await _transporteurService.GetByMatriculeAsync(user.Data);
if (transporteur == null)
{
return NotFound("Transporteur not found");
}
if (transporteur.Data!.VehiculeId != updateDto.VehiculeId)
{
var switchNew = await _vehiculeService.SwitchLibre(updateDto.VehiculeId!);
var switchOld = await _vehiculeService.SwitchLibre(transporteur.Data.VehiculeId!);
if (!switchNew.Success || !switchOld.Success)
{
// Roll back the switch operation if either fails
if (!switchNew.Success)
{
await _vehiculeService.SwitchLibre(transporteur.Data.VehiculeId!);
}
if (!switchOld.Success)
{
await _vehiculeService.SwitchLibre(updateDto.VehiculeId!);
}
return BadRequest("Failed to switch vehicle availability");
}
}
var updatedTransporteur = await _transporteurService.UpdateTransporteur(updateDto, matricule);
return updatedTransporteur.Success ? Ok(updatedTransporteur.Data) : BadRequest(updatedTransporteur.Message);
}
else
{
return BadRequest("Invalid role");
}
}
4 replies
Entity framework exception while creating a many to many relation
Hello everyone,
I've been trying to make a many to many relation between two classes
Voyage
and Transporteur
.
these are the Three classes: c#
public class Transporteur : IdentityUser
{
public string Nom { get; set; } = string.Empty;
public string Prenom { get; set; } = string.Empty;
public Vehicule? Vehicule { get; set; }
public string? VehiculeId { get; set; } = string.Empty;
public bool ExterneDuService { get; set; } = false;
public List<Voyage> Voyages { get; } = [];
public ICollection<TransporteurVoyage> TransporteurVoyages { get; } = [];
}
****************************************************
public class Voyage
{
[Key]
public string IdVoyage { get; set; } = string.Empty;
public DateTime DateSortieBon { get; set; }
public DateTime DateDepartVoyage { get; set; }
public string Etat { get; set; } = string.Empty;
//Demandeur
public Demandeur Demandeur { get; set; } = null!;
public string DemandeurId { get; set; } = string.Empty;
//public ICollection<Utilisateur> Utilisateurs { get; set; } = new List<Utilisateur>();
public Magasinier Magasinier { get; set; } = null!;
public string IdMagasinier { get; set; } = string.Empty;
public TypeVoyage TypeVoyage { get; set; } = null!;
public int TypeVoyageId { get; set; }
public List<Transporteur> Transporteurs { get; } = [];
public ICollection<TransporteurVoyage> TransporteurVoyages { get; } = [];
}
********************************************************************
public class TransporteurVoyage
{
public string TransporteurId { get; set; } = string.Empty;
public string VoyageId { get; set; } = string.Empty;
public bool Panne { get; set; } = false;
public Voyage Voyage { get; set; }
public Transporteur Transporteur { get; set; }
}
c#
public class Transporteur : IdentityUser
{
public string Nom { get; set; } = string.Empty;
public string Prenom { get; set; } = string.Empty;
public Vehicule? Vehicule { get; set; }
public string? VehiculeId { get; set; } = string.Empty;
public bool ExterneDuService { get; set; } = false;
public List<Voyage> Voyages { get; } = [];
public ICollection<TransporteurVoyage> TransporteurVoyages { get; } = [];
}
****************************************************
public class Voyage
{
[Key]
public string IdVoyage { get; set; } = string.Empty;
public DateTime DateSortieBon { get; set; }
public DateTime DateDepartVoyage { get; set; }
public string Etat { get; set; } = string.Empty;
//Demandeur
public Demandeur Demandeur { get; set; } = null!;
public string DemandeurId { get; set; } = string.Empty;
//public ICollection<Utilisateur> Utilisateurs { get; set; } = new List<Utilisateur>();
public Magasinier Magasinier { get; set; } = null!;
public string IdMagasinier { get; set; } = string.Empty;
public TypeVoyage TypeVoyage { get; set; } = null!;
public int TypeVoyageId { get; set; }
public List<Transporteur> Transporteurs { get; } = [];
public ICollection<TransporteurVoyage> TransporteurVoyages { get; } = [];
}
********************************************************************
public class TransporteurVoyage
{
public string TransporteurId { get; set; } = string.Empty;
public string VoyageId { get; set; } = string.Empty;
public bool Panne { get; set; } = false;
public Voyage Voyage { get; set; }
public Transporteur Transporteur { get; set; }
}
39 replies