Exemplo n.º 1
0
        public async Task<IHttpActionResult> Post([FromBody]ContactEditModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            ContactFactory factory = new ContactFactory();
            Contact contact = factory.Create(model);

            _dbContext.Contacts.Add(contact);
            await _dbContext.SaveChangesAsync();

            return CreatedAtRoute("Default", new { id = contact.Id }, Mapper.Map<ContactEditModel>(contact));
        }
Exemplo n.º 2
0
        public async Task<IHttpActionResult> Put(int id, [FromBody]ContactEditModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

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

            Contact contact = await _dbContext.Contacts.FindAsync(model.Id);

            if (contact == null)
            {
                return NotFound();
            }

            _dbContext.Entry(contact).Collection("Tags").Load();
            _dbContext.Entry(contact).Collection("ContactInfos").Load();

            ContactFactory factory = new ContactFactory();
            contact = factory.Update(model, contact, _dbContext);

            await _dbContext.SaveChangesAsync();

            return StatusCode(HttpStatusCode.NoContent);
        }