Exemple #1
0
        public async Task <IActionResult> CreateContact([FromBody] ContactForCreationDto contact, [FromHeader(Name = "Accept")] string mediaType)
        {
            if (contact == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            // Check if the company id and either contactNameToReturn or description already exist
            if (await _contactNameRepo.CheckContactExistsAsync(contact.FirstName, contact.LastName, contact.UserId))
            {
                return(new StatusCodeResult(StatusCodes.Status409Conflict));
            }

            var contactEntity = Mapper.Map <psPARContactName>(contact);

            //get logged in user id from claims
            var userId = User.Claims.FirstOrDefault(c => c.Type == "sub")?.Value;

            //update entity with user id
            contactEntity.IUserId = new Guid(userId);

            await _contactNameRepo.AddNewContactObject(contactEntity);

            if (await _contactNameRepo.Save() == false)
            {
                throw new Exception("Creating an contact failed on save.");
            }

            var contactNameToReturn = Mapper.Map <ContactDto>(contactEntity);

            return(CreatedAtRoute("GetContact", new { contactId = contactNameToReturn.Id, contactNameToReturn.UserId },
                                  mediaType == "application/vnd.persol.hateoas+json" ?
                                  CreateLinksForContact(contactNameToReturn) : contactNameToReturn));
        }
Exemple #2
0
        public async Task <IActionResult> CreateContact([FromBody] ContactForCreationDto contactForCreationDto)
        {
            if (contactForCreationDto == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                var contactEntity = Mapper.Map <Entities.Contact>(contactForCreationDto);

                _registrationRepository.AddContact(contactEntity);

                if (!_registrationRepository.Save())
                {
                    return(StatusCode(500, "A problem happened while handling your request."));
                }

                var createdContactToReturn = Mapper.Map <ContactDto>(contactEntity);

                var email = await _emailService.GenerateEmailMessageAsync(createdContactToReturn);

                await _emailService.SendMessage(email);

                return(CreatedAtRoute("GetContact", new
                                      { id = createdContactToReturn.Id }, createdContactToReturn));
            }
            catch (Exception)
            {
                return(StatusCode(500, "A problem happened while handling your request."));
            }
        }