public dynamic CreateNewContactWithCard(ContactWithCard_ViewModel newContactWithCard) { try { //attempt to create the contact first... var contactToAdd = new Contact()//attempt to create a new object... { ContactId = Guid.NewGuid(), FirstName = newContactWithCard.FirstName, IdCardNum = newContactWithCard.IdCardNum, LastName = newContactWithCard.LastName, SocialSecurityNum = newContactWithCard.SocialSecurityNum }; db.Add(contactToAdd); //attempt to create the associated card... var cardToAdd = new Card()//attempt to create a new object based on the creation of the contact first... { CardId = new Guid(), ContactId = contactToAdd.ContactId, RegisteredOn = DateTime.Now, LastRechargedOn = DateTime.Now, ChargeExpiresOn = DateTime.Now, }; db.Add(cardToAdd); var savingResult = db.SaveChanges(); if (savingResult != 0)//check if an error has occured... { return new ContactWithCard_Created_ViewModel() { NewContactId = contactToAdd.ContactId, NewCardId = cardToAdd.CardId } } ; return(false); } catch (Exception e) { return(false); } }
public IActionResult CreateNewContact_WithCard([FromBody] ContactWithCard_ViewModel newContactWithCard) { try { if (ModelState.IsValid)//checking model state { var contactWithCard_CreationResult = ContactsRepo.CreateNewContactWithCard(newContactWithCard); if (contactWithCard_CreationResult.GetType() == typeof(ContactWithCard_Created_ViewModel)) { return(Ok(contactWithCard_CreationResult)); //if the creation is successful return the id of the new contact as well as the id of the new card... } return(BadRequest()); //if not... return bad request... } return(BadRequest(ModelState)); //if model state is not valid } catch (Exception e) { return(StatusCode(500, _config["StatusCodesText:ServerErr"])); } }