示例#1
0
        public async Task <IActionResult> PostAfschrijving([FromBody] AfschrijvingPostModel afschrijvingPM)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            Afschrijving afschrijving = new Afschrijving()
            {
                LaatstGewijzigd     = DateTime.Now,
                Aankoopbedrag       = afschrijvingPM.Aankoopbedrag,
                Aankoopdatum        = afschrijvingPM.Aankoopdatum,
                VerwachteLevensduur = afschrijvingPM.VerwachteLevensduur,
                Garantie            = afschrijvingPM.Garantie,
                Factuur             = afschrijvingPM.Factuur,
                FactuurNaam         = afschrijvingPM.FactuurNaam,
                AfschrijvingLabels  = new List <AfschrijvingLabel>()
            };

            foreach (var newLabelId in afschrijvingPM.Label)
            {
                Label label = _context.Label.Where(l => l.Id == newLabelId).First();
                afschrijving.AfschrijvingLabels.Add
                (
                    nieuwAfschrijvingLabel(afschrijving, label)
                );
            }

            _context.Afschrijving.Add(afschrijving);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetAfschrijving", new { id = afschrijving.Id }, afschrijving));
        }
示例#2
0
        public async Task <IActionResult> PutAfschrijving([FromRoute] int id, [FromBody] AfschrijvingPostModel afschrijvingPM)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != afschrijvingPM.Id)
            {
                return(BadRequest());
            }

            Afschrijving afschrijving = _context.Afschrijving.Where(a => a.Id == id).Include(a => a.AfschrijvingLabels).First();

            afschrijving.LaatstGewijzigd     = DateTime.Now;
            afschrijving.Aankoopbedrag       = afschrijvingPM.Aankoopbedrag;
            afschrijving.Aankoopdatum        = afschrijvingPM.Aankoopdatum;
            afschrijving.VerwachteLevensduur = afschrijvingPM.VerwachteLevensduur;
            afschrijving.Garantie            = afschrijvingPM.Garantie;
            afschrijving.Factuur             = afschrijvingPM.Factuur;
            afschrijving.FactuurNaam         = afschrijvingPM.FactuurNaam;

            afschrijving.AfschrijvingLabels.Clear();

            foreach (var newLabelId in afschrijvingPM.Label)
            {
                Label label = _context.Label.Where(l => l.Id == newLabelId).First();
                afschrijving.AfschrijvingLabels.Add
                (
                    nieuwAfschrijvingLabel(afschrijving, label)
                );
            }

            _context.Entry(afschrijving).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!AfschrijvingExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }