public async Task <IActionResult> PostInkomst([FromBody] InkomstPostModel inkomstVM) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Inkomst inkomst = new Inkomst { Persoon = inkomstVM.Persoon, Bedrag = inkomstVM.Bedrag, Begindatum = inkomstVM.Begindatum, Einddatum = inkomstVM.Einddatum, Interval = inkomstVM.Interval, LaatstGewijzigd = DateTime.Now, InkomstLabels = new List <InkomstLabel>() }; foreach (var newLabelId in inkomstVM.Label) { Label label = _context.Label.Where(l => l.Id == newLabelId).First(); inkomst.InkomstLabels.Add ( nieuwInkomstLabel(inkomst, label) ); } _context.Inkomst.Add(inkomst); await _context.SaveChangesAsync(); return(CreatedAtAction("GetInkomst", new { id = inkomst.Id }, inkomst)); }
private InkomstLabel nieuwInkomstLabel(Inkomst inkomst, Label label) { return(new InkomstLabel { Inkomst = inkomst, Label = label, InkomstId = inkomst.Id, LabelId = label.Id }); }
public async Task <IActionResult> PutInkomst([FromRoute] int id, [FromBody] InkomstPostModel inkomstVM) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != inkomstVM.Id) { return(BadRequest()); } Inkomst inkomst = _context.Inkomst.Where(i => i.Id == id).Include(i => i.InkomstLabels).First(); inkomst.Persoon = inkomstVM.Persoon; inkomst.Bedrag = inkomstVM.Bedrag; inkomst.Begindatum = inkomstVM.Begindatum; inkomst.Einddatum = inkomstVM.Einddatum; inkomst.Interval = inkomstVM.Interval; inkomst.LaatstGewijzigd = DateTime.Now; inkomst.InkomstLabels.Clear(); foreach (var newLabelId in inkomstVM.Label) { Label label = _context.Label.Where(l => l.Id == newLabelId).First(); inkomst.InkomstLabels.Add ( nieuwInkomstLabel(inkomst, label) ); } _context.Entry(inkomst).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!InkomstExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> GetInkomst([FromRoute] int id) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Inkomst inkomst = await _context.Inkomst .Where(i => i.Id == id) .Include(i => i.PersoonNavigation) .Include(i => i.InkomstLabels) .ThenInclude(inkomstLabel => inkomstLabel.Label) .FirstOrDefaultAsync(); if (inkomst == null) { return(NotFound()); } InkomstViewModel inkomstVM = new InkomstViewModel { Id = inkomst.Id, Persoon = inkomst.PersoonNavigation != null ? new PersoonViewModel { Id = inkomst.PersoonNavigation.Id, Voornaam = inkomst.PersoonNavigation.Voornaam, Achternaam = inkomst.PersoonNavigation.Achternaam } : null, Bedrag = inkomst.Bedrag, Begindatum = inkomst.Begindatum, Einddatum = inkomst.Einddatum, Interval = inkomst.Interval, Label = inkomstLabelNaarLabel(inkomst.InkomstLabels) }; return(Ok(inkomstVM)); }