Exemplo n.º 1
0
        public async Task <UserViewModel> AddOrUpdateUserFields(UserAddFieldRequest request)
        {
            User user = await userRepository.GetByIdAsync(ObjectId.Parse(request.UserId));

            if (user != null)
            {
                FilterDefinition <ObjectLevel> buidler = Builders <ObjectLevel> .Filter.Eq("object_id", request.UserId);

                List <ObjectLevel> existObjectLevels = (await objectLevelRepository.FindListAsync(buidler)).ToList();

                foreach (string item in request.FieldId)
                {
                    var existObjectLevel = existObjectLevels.FirstOrDefault(x => x.FieldId == item);
                    if (existObjectLevel != null)
                    {
                        if (existObjectLevel.IsActive == false)
                        {
                            existObjectLevel.IsActive = true;
                        }

                        existObjectLevel.ModifiedDate = DateTime.Now;
                        await objectLevelRepository.UpdateAsync(existObjectLevel, existObjectLevel.Id);
                    }
                    else
                    {
                        ObjectLevel objectLevel = new ObjectLevel()
                        {
                            LevelId  = Constants.LevelConstants.LEVEL_0_ID,
                            FieldId  = item,
                            ObjectId = user.OId,
                            Point    = 0
                        };
                        await objectLevelRepository.AddAsync(objectLevel);
                    }
                }

                foreach (var ex in existObjectLevels)
                {
                    if (request.FieldId.FirstOrDefault(x => x == ex.FieldId) == null)
                    {
                        ex.IsActive     = false;
                        ex.ModifiedDate = DateTime.Now;
                        await objectLevelRepository.UpdateAsync(ex, ex.Id);
                    }
                }
                return(mapper.Map <UserViewModel>(user));
            }
            else
            {
                throw new Exception("Không tìm thấy user");
            }
        }
Exemplo n.º 2
0
        public async Task UpdateUserPoint(string userId, string fieldId, int point)
        {
            var builder = Builders <ObjectLevel> .Filter;
            var filter  = builder.Eq("object_id", userId)
                          & builder.Eq("field_id", fieldId)
                          & builder.Eq("is_active", true);
            var objectLevel = await objectLevelRepository.FindAsync(filter);

            if (objectLevel != null)
            {
                objectLevel.Point += point;

                if (objectLevel.Point <= 0)
                {
                    objectLevel.Point = 0;
                }

                if (objectLevel.Point < LevelPoint.Level1)
                {
                    objectLevel.LevelId = levelRepository.GetAll().FirstOrDefault(x => x.Order == 0)?.OId;
                }
                else if (objectLevel.Point < LevelPoint.Level2)
                {
                    objectLevel.LevelId = levelRepository.GetAll().FirstOrDefault(x => x.Order == 1)?.OId;
                }
                else if (objectLevel.Point < LevelPoint.Level3)
                {
                    objectLevel.LevelId = levelRepository.GetAll().FirstOrDefault(x => x.Order == 2)?.OId;
                }
                else if (objectLevel.Point < LevelPoint.Level4)
                {
                    objectLevel.LevelId = levelRepository.GetAll().FirstOrDefault(x => x.Order == 3)?.OId;
                }
                else
                {
                    objectLevel.LevelId = levelRepository.GetAll().FirstOrDefault(x => x.Order == 4)?.OId;
                }

                await objectLevelRepository.UpdateAsync(objectLevel, objectLevel.Id);
            }
        }