public void AddCar(MasinaAdd car, Guid?userId, byte[] poza) { if (userId == null) { userId = GetUser(); } if (userId == Guid.Empty) { return; } car.Id = Guid.NewGuid(); var sql = "INSERT INTO public.anunturi VALUES(@id, @caroserie, @cutie, @transmisie, @normapoluare, @combustibil, @cp, @capacitatecilindrica," + "@km, @pret, @anfabricatie, @marca, @model, @descriere, @proprietarid, @poza)"; using (var connection = new NpgsqlConnection(connectionString)) { using (var command = new NpgsqlCommand(sql, connection)) { connection.Open(); command.Parameters.AddWithValue("id", car.Id); command.Parameters.AddWithValue("caroserie", (int)car.Caroserie); command.Parameters.AddWithValue("cutie", (int)car.Cutie); command.Parameters.AddWithValue("transmisie", (int)car.Transmisie); command.Parameters.AddWithValue("normapoluare", (int)car.NormaPoluare); command.Parameters.AddWithValue("combustibil", (int)car.Combustibil); command.Parameters.AddWithValue("cp", car.CP); command.Parameters.AddWithValue("capacitatecilindrica", car.CapacitateCilindrica); command.Parameters.AddWithValue("km", car.Km); command.Parameters.AddWithValue("pret", car.Pret); command.Parameters.AddWithValue("anfabricatie", car.AnFabricatie); command.Parameters.AddWithValue("marca", car.Marca); command.Parameters.AddWithValue("model", car.Model); command.Parameters.AddWithValue("descriere", car.Descriere); command.Parameters.AddWithValue("proprietarid", userId); command.Parameters.AddWithValue("poza", poza); command.ExecuteNonQuery(); } } }
public IActionResult AddCar(MasinaAdd car) { byte[] content = null; if (car.Poza != null) { if (car.Poza.Length > 0) { using (var fs = car.Poza.OpenReadStream()) using (var ms = new MemoryStream()) { fs.CopyTo(ms); content = ms.ToArray(); } } } services.AddCar(car, currentUser?.Id, content); return(RedirectToAction("ViewCars")); }
public IActionResult AddCar() { var model = new MasinaAdd(); return(View(model)); }