public IActionResult Delete([FromBody] ClientDeleteVM client) { if (!ModelState.IsValid) { return(BadRequest()); } var clients = _context.Clients.SingleOrDefault(p => p.Id == client.Id); if (clients != null) { _context.Clients.Remove(clients); _context.SaveChanges(); } return(Ok()); }
public string Delete(ClientDeleteVM client) { var http = (HttpWebRequest)WebRequest.Create(new Uri(_url)); http.Accept = "application/json"; http.ContentType = "application/json"; http.Method = "DELETE"; string parsedContent = JsonConvert.SerializeObject(client); UTF8Encoding encoding = new UTF8Encoding(); Byte[] bytes = encoding.GetBytes(parsedContent); Stream newStream = http.GetRequestStream(); newStream.Write(bytes, 0, bytes.Length); newStream.Close(); var response = http.GetResponse(); var stream = response.GetResponseStream(); var sr = new StreamReader(stream); var content = sr.ReadToEnd(); return content; }
public IActionResult Delete(ClientDeleteVM model) { try { if (!ModelState.IsValid) { Response.StatusCode = 400; return(PartialView("_Delete", model)); } var client = _context.Clients.Where(x => x.ClientId == model.ClientId).FirstOrDefault(); if (client == null) { return(RedirectToAction("Index")); } var appointmentsServices = _context.AppointmentsServices.Include(x => x.Appointment) .Where(x => x.Appointment.ClientId == client.ClientId) .ToArray(); var appointments = _context.Appointments.Where(x => x.ClientId == client.ClientId).ToArray(); _context.AppointmentsServices.RemoveRange(appointmentsServices); _context.Appointments.RemoveRange(appointments); _context.Remove(client); _context.SaveChanges(); return(PartialView("_Delete", model)); } catch (Exception ex) { return(RedirectToAction("Index")); } }
public Task<string> DeleteAsync(ClientDeleteVM client) { return Task.Run(() => Delete(client)); }