Inheritance: IRegistrationInfo
Exemplo n.º 1
0
        public RegistrationInfo GetItem(int itemId, int codeCampId)
        {
            RegistrationInfo i = null;

            using (IDataContext ctx = DataContext.Instance())
            {
                var rep = ctx.GetRepository <RegistrationInfo>();
                i = rep.GetById(itemId, codeCampId);
            }
            return(i);
        }
        public HttpResponseMessage CreateRegistration(RegistrationInfo registration)
        {
            try
            {
                var response = new ServiceResponse<RegistrationInfo>();

                if (registration.UserId <= 0)
                {
                    // user isn't logged in and therefore doesn't likely have a user account on the site
                    // we need to register them into DNN for them

                    var firstName = registration.CustomPropertiesObj.FirstOrDefault(p => p.Name == "FirstName").Value;
                    var lastName = registration.CustomPropertiesObj.FirstOrDefault(p => p.Name == "LastName").Value;
                    var email = registration.CustomPropertiesObj.FirstOrDefault(p => p.Name == "Email").Value;
                    var portalId = int.Parse(registration.CustomPropertiesObj.FirstOrDefault(p => p.Name == "PortalId").Value);

                    var ctlUser = new DnnUserController();
                    var status = ctlUser.CreateNewUser(firstName, lastName, email, portalId);

                    switch (status)
                    {
                        case UserCreateStatus.DuplicateEmail:
                            ServiceResponseHelper<RegistrationInfo>.AddUserCreateError("DuplicateEmail", ref response);
                            break;
                        case UserCreateStatus.DuplicateUserName:
                            ServiceResponseHelper<RegistrationInfo>.AddUserCreateError("DuplicateUserName", ref response);
                            break;
                        case UserCreateStatus.Success:
                            var user = UserController.GetUserByName(email);
                            registration.UserId = user.UserID;

                            UserController.UserLogin(portalId, user, PortalSettings.PortalName, string.Empty, false);
                            break;
                        case UserCreateStatus.UnexpectedError:
                            ServiceResponseHelper<RegistrationInfo>.AddUserCreateError("UnexpectedError", ref response);
                            break;
                        case UserCreateStatus.UsernameAlreadyExists:
                            ServiceResponseHelper<RegistrationInfo>.AddUserCreateError("UsernameAlreadyExists", ref response);
                            break;
                        case UserCreateStatus.UserAlreadyRegistered:
                            ServiceResponseHelper<RegistrationInfo>.AddUserCreateError("UserAlreadyRegistered", ref response);
                            break;
                        default:
                            ServiceResponseHelper<RegistrationInfo>.AddUnknownError(ref response);
                            break;
                    }
                }

                if (response.Errors.Count == 0)
                {
                    registration.RegistrationDate = DateTime.Now;

                    RegistrationDataAccess.CreateItem(registration);

                    var registrations = RegistrationDataAccess.GetItems(registration.CodeCampId).OrderByDescending(r => r.RegistrationId);
                    var savedRegistration = registrations.FirstOrDefault(r => r.UserId == registration.UserId);

                    response.Content = savedRegistration;

                    if (savedRegistration == null)
                    {
                        ServiceResponseHelper<RegistrationInfo>.AddNoneFoundError("registration", ref response);
                    }
                }

                return Request.CreateResponse(HttpStatusCode.OK, response.ObjectToJson());
            }
            catch (Exception ex)
            {
                Exceptions.LogException(ex);
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ERROR_MESSAGE);
            }
        }
        public HttpResponseMessage UpdateRegistration(RegistrationInfo registration)
        {
            try
            {
                var originalRegistration = RegistrationDataAccess.GetItemByUserId(registration.UserId, registration.CodeCampId);
                var updatesToProcess = false;

                // only update the fields that would be udpdated from the UI to keep the DB clean

                if (originalRegistration.ShirtSize != registration.ShirtSize)
                {
                    originalRegistration.ShirtSize = registration.ShirtSize;
                    updatesToProcess = true;
                }

                if (originalRegistration.HasDietaryRequirements != registration.HasDietaryRequirements)
                {
                    originalRegistration.HasDietaryRequirements = registration.HasDietaryRequirements;
                    updatesToProcess = true;
                }

                if (originalRegistration.Notes != registration.Notes)
                {
                    originalRegistration.Notes = registration.Notes;
                    updatesToProcess = true;
                }

                if (originalRegistration.CustomProperties != null)
                {
                    // parse custom properties for updates
                    foreach (var property in originalRegistration.CustomPropertiesObj)
                    {
                        if (registration.CustomPropertiesObj.Any(p => p.Name == property.Name))
                        {
                            // see if the existing property needs to be updated
                            var prop = registration.CustomPropertiesObj.FirstOrDefault(p => p.Name == property.Name);
                            if (!string.Equals(prop.Value, property.Value))
                            {
                                property.Value = prop.Value;
                                updatesToProcess = true;
                            }
                        }
                        else
                        {
                            // delete the property
                            originalRegistration.CustomPropertiesObj.Remove(property);
                            updatesToProcess = true;
                        }
                    }
                }

                if (registration.CustomPropertiesObj != null)
                {
                    // add any new properties
                    if (originalRegistration.CustomProperties == null)
                    {
                        foreach (var property in registration.CustomPropertiesObj)
                        {
                            originalRegistration.CustomPropertiesObj.Add(property);
                            updatesToProcess = true;
                        }
                    }
                    else
                    {
                        foreach (var property in registration.CustomPropertiesObj.Where(property => !originalRegistration.CustomPropertiesObj.Contains(property)))
                        {
                            originalRegistration.CustomPropertiesObj.Add(property);
                            updatesToProcess = true;
                        }
                    }
                }

                if (updatesToProcess)
                {
                    RegistrationDataAccess.UpdateItem(originalRegistration);
                }

                var response = new ServiceResponse<string> { Content = SUCCESS_MESSAGE };

                return Request.CreateResponse(HttpStatusCode.OK, response.ObjectToJson());
            }
            catch (Exception ex)
            {
                Exceptions.LogException(ex);
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ERROR_MESSAGE);
            }
        }
 public void UpdateItem(RegistrationInfo i)
 {
     repo.UpdateItem(i);
 }
 public void DeleteItem(RegistrationInfo i)
 {
     repo.DeleteItem(i);
 }
 public void CreateItem(RegistrationInfo i)
 {
     repo.CreateItem(i);
 }
 public void UpdateItem(RegistrationInfo i)
 {
     repo.UpdateItem(i);
 }
 public void DeleteItem(RegistrationInfo i)
 {
     repo.DeleteItem(i);
 }
 public void CreateItem(RegistrationInfo i)
 {
     repo.CreateItem(i);
 }