Beispiel #1
0
        public async Task <IActionResult> PutContact(int id, PublicApi.v1.DTO.Contact contact)
        {
            if (id != contact.Id)
            {
                return(BadRequest());
            }

            // check, that the Person being used is really belongs to logged in user
            if (!await _bll.Contacts.BelongsToUserAsync(id, User.GetUserId()))
            {
                return(NotFound());
            }

            _bll.Contacts.Update(PublicApi.v1.Mappers.ContactMapper.MapFromExternal(contact));
            await _bll.SaveChangesAsync();

            return(NoContent());
        }
Beispiel #2
0
        public async Task <ActionResult <PublicApi.v1.DTO.Contact> > PostContact(PublicApi.v1.DTO.Contact contact)
        {
            // check, that the Person being used is really belongs to logged in user
            if (!await _bll.Persons.BelongsToUserAsync(contact.PersonId, User.GetUserId()))
            {
                return(NotFound());
            }

            // get the enitity back with attached state id - (- maxint)
            contact = PublicApi.v1.Mappers.ContactMapper.MapFromBLL(
                _bll.Contacts.Add(PublicApi.v1.Mappers.ContactMapper.MapFromExternal(contact)));
            // ef will update its internally tracked entities
            await _bll.SaveChangesAsync();

            // get the updated entity, now with ID from database
            contact = PublicApi.v1.Mappers.ContactMapper.MapFromBLL(
                _bll.Contacts.GetUpdatesAfterUOWSaveChanges(
                    PublicApi.v1.Mappers.ContactMapper.MapFromExternal(contact)));


            return(CreatedAtAction("GetContact", new { id = contact.Id }, contact));
        }