public Addressget UpdateAddress(Addressget addressget, int IdPostulant) { var sql = "SELECT * FROM Location Where Location = @Location"; var sql2 = "INSERT INTO Location (Location) VALUES (@NameLocation)"; var sql3 = "UPDATE Address SET Home = @Home, Number = @Number, PostalCode = @PostalCode, IdLocation = @IdLocation WHERE Id = @Id"; var sql4 = "SELECT * FROM Postulant WHERE Id = @Id"; int?IdLocation = 0; int?IdAddress = 0; using (var conn = new SqlConnection(connStr)) { conn.Open(); IdLocation = ThereisLocation(addressget.Location); if (IdLocation != null && IdLocation != 0) { if (addressget.Id != null && addressget.Id != 0) { Address address = fnc.createAddress(addressget, IdLocation); conn.Execute(sql3, address); } else { IdAddress = (int)conn.Query(sql4, new { @Id = IdPostulant }).FirstOrDefault().IdAddress; Address address = fnc.createAddress(addressget, IdLocation); address.Id = IdAddress; conn.Execute(sql3, address); } } else { conn.Execute(sql2, new { @NameLocation = addressget.Location }); IdLocation = (int)conn.Query(sql).FirstOrDefault().Id; if (addressget.Id != null && addressget.Id != 0) { Address address = fnc.createAddress(addressget, IdLocation); conn.Execute(sql3, address); } else { IdAddress = (int)conn.Query(sql4, new { @Id = IdPostulant }).FirstOrDefault().IdAddress; Address address = fnc.createAddress(addressget, IdLocation); address.Id = IdAddress; conn.Execute(sql3, address); } } conn.Close(); } return(getAddress(IdPostulant)); }
public Address createAddress(Addressget addressget, int?IdLocation) { Address address = new Address { Id = addressget.Id, Home = addressget.Home, Number = addressget.Number, PostalCode = addressget.PostalCode, IdLocation = IdLocation }; return(address); }
//POST api/Management/Address public ActionResult InsertAddress([FromBody] Addressget addressget) { Postulant post = dbm.GetPostulant(addressget.IdPostulant); if (this.ModelState.IsValid && post.IdAddress == 0) { dbm.InsertAddress(addressget); return(Ok()); } else { return(BadRequest()); } }
//ADDRESS public void InsertAddress(Addressget addressget) { var sql = "SELECT * FROM Location WHERE Location = @Location"; var sql2 = "INSERT INTO Location (Location) VALUES (@NameLocation)"; var sql3 = "INSERT INTO Address (Home, Number, PostalCode, IdLocation) VALUES (@Home, @Number, @PostalCode, @IdLocation)"; var sql4 = "SELECT TOP 1 * FROM Address ORDER BY Id DESC"; var sql5 = "UPDATE Postulant SET IdAddress = @IdAddress WHERE Id = @Id"; int?IdLocation = 0; int?IdAddress = 0; using (var conn = new SqlConnection(connStr)) { conn.Open(); IdLocation = ThereisLocation(addressget.Location); if (IdLocation != null && IdLocation != 0) { Address address = fnc.createAddress(addressget, IdLocation); conn.Execute(sql3, address); IdAddress = (int)conn.Query(sql4).FirstOrDefault().Id; conn.Execute(sql5, new { @IdAddress = IdAddress, @Id = addressget.IdPostulant }); } else { conn.Execute(sql2, new { @NameLocation = addressget.Location }); IdLocation = (int)conn.Query(sql, new { @Location = addressget.Location }).FirstOrDefault().Id; Address address = fnc.createAddress(addressget, IdLocation); conn.Execute(sql3, address); IdAddress = (int)conn.Query(sql4).FirstOrDefault().Id; conn.Execute(sql5, new { @IdAddress = IdAddress, @Id = addressget.IdPostulant }); } conn.Close(); } }
//PUT api/Management/Address/Modif/{IdPostulant} public Addressget UpdateAddress([FromBody] Addressget address, int IdPostulant) { return(dbm.UpdateAddress(address, IdPostulant)); }