public async Task <IActionResult> DeleteBrugerKurser(string id, int kid) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { var user = _context.Brugere.Find(id); if (user == null) { return(NotFound()); } var kursus = _context.Kurser.Find(kid); if (kursus == null) { return(NotFound()); } BrugerKurser k = new BrugerKurser { KursusId = kid, Uid = id.ToString(), Kursus = kursus, U = user }; _context.BrugerKurser.Remove(k); await _context.SaveChangesAsync(); return(Ok(k)); } catch (Exception) { return(NotFound()); } }
public async Task <IActionResult> PutBrugerKurser([FromRoute] int id, [FromBody] BrugerKurser brugerKurser) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != brugerKurser.KursusId) { return(BadRequest()); } _context.Entry(brugerKurser).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!BrugerKurserExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PostBrugerKurser([FromBody] BrugerKurser brugerKurser) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } _context.BrugerKurser.Add(brugerKurser); try { await _context.SaveChangesAsync(); } catch (DbUpdateException) { if (BrugerKurserExists(brugerKurser.KursusId)) { return(new StatusCodeResult(StatusCodes.Status409Conflict)); } else { throw; } } return(CreatedAtAction("GetBrugerKurser", new { id = brugerKurser.KursusId }, brugerKurser)); }
public async Task <IActionResult> PostBrugerKurser(string id, int kid) { if (string.IsNullOrEmpty(id)) { return(BadRequest()); } if (kid < 0) { return(BadRequest()); } var user = _context.Brugere.Find(id); var kursus = _context.Kurser.Find(kid); BrugerKurser k = new BrugerKurser { KursusId = kid, Uid = id.ToString(), Kursus = kursus, U = user }; if (!ModelState.IsValid) { return(BadRequest(ModelState)); } _context.BrugerKurser.Add(k); try { await _context.SaveChangesAsync(); } catch (DbUpdateException) { if (BrugerKurserExists(k.KursusId)) { return(new StatusCodeResult(StatusCodes.Status409Conflict)); } else { throw; } } return(Ok()); }