예제 #1
0
        public HttpStatusCode UpdatePrimaryContact(PrimaryContact contact)
        {
            try
            {
                var contactToUpdate     = _db.PrimaryContacts.SingleOrDefault(c => c.Id == contact.Id);
                var contactBeforeUpdate = DtoHelper.CreatePrimaryContactDto(contactToUpdate);

                if (contactToUpdate != null)
                {
                    contactToUpdate.Name       = contact.Name;
                    contactToUpdate.Email      = contact.Email;
                    contactToUpdate.Telephone  = contact.Telephone;
                    contactToUpdate.Function   = contact.Function;
                    contactToUpdate.Salutation = contact.Salutation;
                    contactToUpdate.Fax        = contact.Fax;
                    contactToUpdate.Active     = contact.Active;
                }

                _db.SaveChanges();

                _mongoMongoLogger.SuccessfulUpdateServerLog(HttpContext.Current.User, contactBeforeUpdate, DtoHelper.CreatePrimaryContactDto(contactToUpdate));

                return(HttpStatusCode.OK);
            }
            catch (Exception ex)
            {
                _mongoMongoLogger.FailedUpdateServerLog <Exception, object, object>(HttpContext.Current.User, ex.Message,
                                                                                    ex.InnerException, contact);

                return(HttpStatusCode.InternalServerError);
            }
        }
        public IHttpActionResult AddNewPrimaryContact([FromBody] PrimaryContact newContact)
        {
            if (Equals(newContact, null))
            {
                return(BadRequest());
            }

            var contactAlreadyExists = _unitOfWork.PrimaryContacts.Find(pc => pc.Email == newContact.Email);

            if (!contactAlreadyExists.Any())
            {
                try
                {
                    _unitOfWork.PrimaryContacts.Add(newContact);
                    _unitOfWork.Complete();

                    var createdContact = _unitOfWork.PrimaryContacts.FindSingle(pc => pc.Email == newContact.Email);

                    return(Ok(DtoHelper.CreatePrimaryContactDto(createdContact)));
                }
                catch (Exception ex)
                {
                    _logger.FailedUpdateServerLog <Exception, object, object>(HttpContext.Current.User, ex.Message,
                                                                              ex.InnerException, DtoHelper.CreatePrimaryContactDto(newContact));

                    return(InternalServerError(ex));
                }
            }
            else
            {
                return(Conflict());
            }
        }
        public IHttpActionResult GetPrimaryContactById(int id)
        {
            var contact = _unitOfWork.PrimaryContacts.FindSingle(pc => pc.Id == id);

            if (Equals(contact, null))
            {
                return(BadRequest());
            }

            return(Ok(DtoHelper.CreatePrimaryContactDto(contact)));
        }
        public IHttpActionResult DeactivatePrimaryContact(int deactivateId, int replacementId)
        {
            if (deactivateId <= 0 || replacementId <= 0)
            {
                return(BadRequest());
            }

            var toDeactivate = _unitOfWork.PrimaryContacts.FindSingle(pc => pc.Id == deactivateId);
            var toReplace    = _unitOfWork.PrimaryContacts.FindSingle(pc => pc.Id == replacementId);

            if (Equals(toDeactivate, null) || Equals(toReplace, null))
            {
                return(BadRequest());
            }

            toDeactivate.Active = false;
            toReplace.Active    = true;

            var barcodesToUpdate = _unitOfWork.Glns.Find(gln => gln.ContactId == deactivateId);

            if (barcodesToUpdate.Any())
            {
                barcodesToUpdate.ForEach(bc => bc.ContactId = replacementId);
            }

            toDeactivate.Active = false;

            try
            {
                _unitOfWork.Complete();

                _logger.SuccessfulReplacementServerLog(HttpContext.Current.User, DtoHelper.CreatePrimaryContactDto(toDeactivate), DtoHelper.CreatePrimaryContactDto(toReplace));

                return(Ok(DtoHelper.CreatePrimaryContactDto(toDeactivate)));
            }
            catch (Exception ex)
            {
                _logger.FailedUpdateServerLog(HttpContext.Current.User, ex.Message, ex.InnerException, $"Attempt to deactivate Primary Contact Id: {deactivateId}", $"Supplied replacement Primary Contact Id: {replacementId}");

                return(InternalServerError(ex));
            }
        }
        public IHttpActionResult UpdatePrimaryContact(PrimaryContact contact)
        {
            if (Equals(contact, null))
            {
                return(BadRequest());
            }

            var contactToUpdate = _unitOfWork.PrimaryContacts.FindSingle(pc => pc.Id == contact.Id);

            if (Equals(contactToUpdate, null))
            {
                return(BadRequest());
            }

            var contactBeforeUpdate = DtoHelper.CreatePrimaryContactDto(contactToUpdate);

            try
            {
                contactToUpdate.Name       = contact.Name;
                contactToUpdate.Email      = contact.Email;
                contactToUpdate.Telephone  = contact.Telephone;
                contactToUpdate.Function   = contact.Function;
                contactToUpdate.Salutation = contact.Salutation;
                contactToUpdate.Fax        = contact.Fax;
                contactToUpdate.Active     = contact.Active;
                contactToUpdate.Version    = contactToUpdate.Version + 1;

                _unitOfWork.Complete();

                _logger.SuccessfulUpdateServerLog(HttpContext.Current.User, contactBeforeUpdate, DtoHelper.CreatePrimaryContactDto(_unitOfWork.PrimaryContacts.FindSingle(pc => pc.Id == contact.Id)));

                return(Ok(DtoHelper.CreatePrimaryContactDto(contactToUpdate)));
            }
            catch (Exception ex)
            {
                _logger.FailedUpdateServerLog <Exception, object, object>(HttpContext.Current.User, ex.Message,
                                                                          ex.InnerException, contact);

                return(InternalServerError());
            }
        }
예제 #6
0
        public HttpStatusCode DeactivatePrimaryContact(int deactivateId, int replacementId)
        {
            var toDeactivate = _db.PrimaryContacts.SingleOrDefault(pc => pc.Id == deactivateId);
            var toReplace    = _db.PrimaryContacts.SingleOrDefault(pc => pc.Id == replacementId);

            if (Equals(toDeactivate, null) || Equals(toReplace, null))
            {
                return(HttpStatusCode.BadRequest);
            }

            toDeactivate.Active = false;
            toReplace.Active    = true;

            var barcodesToUpdate = _db.Glns.Where(bc => bc.ContactId == deactivateId).ToList();

            if (!Equals(barcodesToUpdate, null))
            {
                barcodesToUpdate.ForEach(bc => bc.ContactId = replacementId);
            }

            toDeactivate.Active = false;

            try
            {
                _db.SaveChanges();

                _mongoMongoLogger.SuccessfulUpdateServerLog(HttpContext.Current.User, DtoHelper.CreatePrimaryContactDto(toDeactivate), DtoHelper.CreatePrimaryContactDto(toReplace));

                return(HttpStatusCode.OK);
            }
            catch (Exception ex)
            {
                _mongoMongoLogger.FailedUpdateServerLog(HttpContext.Current.User, ex.Message, ex.InnerException, $"Attempt to deactivate Primary Contact Id: {deactivateId}", $"Supplied replacement Primary Contact Id: {replacementId}");

                return(HttpStatusCode.InternalServerError);
            }
        }