Beispiel #1
0
        public BooleanResponse SaveEventCategories(List<EventCategory> eventCategories)
        {
            var response = new BooleanResponse();

            try
            {
                var clnEventCategories = this.StatWinnerDatabase.GetCollection<EventCategoryDBEntity>("NotificationEventCategories");

                var existingList = clnEventCategories.Find(_ => true).ToListAsync().Result;
                var eventCategoriesToSave =
                    eventCategories.Select(EventManagementFactory.ConvertToEventCategoryEntity).ToList();

                var insertList = eventCategoriesToSave.Where(e => e.Id == ObjectId.Empty).ToList();
                insertList.ForEach(i => i.Id = ObjectId.GenerateNewId());

                var deleteList =
                    existingList.Where(l => eventCategories.All(ec => ec.Id != l.Id.ToString())).ToList();
                var deleteFilter = Builders<EventCategoryDBEntity>.Filter.In("_id", deleteList.Select(d => d.Id));
                var deleteResponse = clnEventCategories.DeleteManyAsync(deleteFilter);

                //Iterate over existing elemnts and replace
                foreach (
                    var setting in
                        eventCategoriesToSave.Where(cs => cs.Id != ObjectId.Empty)
                    )
                {
                    var updateFilter = Builders<EventCategoryDBEntity>.Filter.Eq("_id", setting.Id);
                    var replaceResponse = clnEventCategories.ReplaceOneAsync(updateFilter, setting);
                }

                var insertResponse = clnEventCategories.InsertManyAsync(insertList);

                return new BooleanResponse()
                {
                    Result = true
                };
            }
            catch (Exception ex)
            {
                response.SetError(ex);
            }

            return response;
        }
        /// <summary>
        /// Signs user in the system
        /// </summary>
        /// <param name="signInUser"></param>
        /// <returns></returns>
        public async Task<BooleanResponse> SignInUser(SignInRequest signInUser)
        {
            var response = new BooleanResponse();
            
            try
            {

                const string errorMessage = "Invalid Email or Password";

                var user = await UserManager.FindByNameAsync(signInUser.Email);
                if (user == null)
                {
                    response.SetError(errorMessage);
                }

                var isValidUser = await UserManager.CheckPasswordAsync(user, signInUser.Password);

                if (isValidUser)
                {
                    AuthManager.SignOut();
                    var identity = await user.GenerateUserIdentityAsync(UserManager);

                    if (signInUser.RememberMe)
                    {
                        var rememberBrowserIdentity = AuthManager.CreateTwoFactorRememberBrowserIdentity(user.Id);
                        AuthManager.SignIn(new AuthenticationProperties { IsPersistent = true }, identity, rememberBrowserIdentity);
                    }
                    else
                    {
                        AuthManager.SignIn(new AuthenticationProperties { IsPersistent = true }, identity);
                    }

                    response.Result = true;
                }
                else
                {
                    response.SetError("");
                }
            }
            catch (Exception ex)
            {
                response.SetError(ex);
            }

            return response;
        }
Beispiel #3
0
        /// <summary>
        /// Save Notification Event
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<BooleanResponse> SaveNotificationEvent(EventEntity entity)
        {
            var response = new BooleanResponse();

            try
            {
                var dbEntity = EventManagementFactory.ConvertToEventDbEntity(entity);

                ApplicationUser appUser = null;

                if (HttpContext.Current.User != null)
                {
                    appUser = await UserManager.FindByNameAsync(HttpContext.Current.User.Identity.Name);
                }

                if (dbEntity.Id == ObjectId.Empty)
                {
                    if (appUser != null)
                    {
                        dbEntity.CreatedBy = new EventUserDbEntity()
                        {
                            UserId = appUser.Id.ToString(),
                            EmailAddress = appUser.Email,
                            FullName = String.Format("{0} {1}", appUser.FirstName, appUser.LastName)
                        };
                    }

                    dbEntity.CreatedDate = DateTime.Now;
                }

                if (appUser != null)
                {
                    dbEntity.ModifiedBy = new EventUserDbEntity()
                    {
                        UserId = appUser.Id.ToString(),
                        EmailAddress = appUser.Email,
                        FullName = String.Format("{0} {1}", appUser.FirstName, appUser.LastName)
                    };
                }

                dbEntity.ModifiedDate = DateTime.Now;

                var clnEvents = this.StatWinnerDatabase.GetCollection<EventDbEntity>("NotificationEvents");
                if (dbEntity.Id == ObjectId.Empty)
                {
                    dbEntity.Id = ObjectId.GenerateNewId();
                    await clnEvents.InsertOneAsync(dbEntity);
                }
                else
                {
                    var updateFilter = Builders<EventDbEntity>.Filter.Eq("_id", dbEntity.Id);
                    await clnEvents.ReplaceOneAsync(updateFilter, dbEntity);
                }
            }
            catch (Exception ex)
            {
                response.SetError(ex);
            }

            return response;
        }
        /// <summary>
        /// Registers user in the system
        /// </summary>
        /// <param name="registerUser"></param>
        /// <returns></returns>
        public async Task<BooleanResponse> UserRegistration(RegisterUser registerUser)
        {
            var response = new BooleanResponse();
            try
            {
                
                var user = new ApplicationUser()
                {
                    City = registerUser.City,
                    CountryId = registerUser.CountryId,
                    Email = registerUser.Email,
                    FirstName = registerUser.FirstName,
                    LastName = registerUser.LastName,

                    State = registerUser.State,
                    UserName = registerUser.Email
                };

                var result = await UserManager.CreateAsync(user, registerUser.Password);

                if (result.Succeeded)
                {
                    user = UserManager.FindByName(registerUser.Email);
                    var identity = await user.GenerateUserIdentityAsync(UserManager);
                    AuthManager.SignIn(new AuthenticationProperties {IsPersistent = true}, identity);
                    response.Result = true;
                }
                else
                {
                    response.Errors.AddRange(result.Errors.Select(s => new Error() { Message = s}));
                }
            }
            catch (Exception ex)
            {
                response.SetError(ex);
            }

            return response;
        }