示例#1
0
        /// <summary>
        ///  Validate User ...
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public Operation <UserModel> ValidateUser(string username, string password)
        => Operation.Create(() =>
        {
            var query = from user in _db.Set <User>()
                        join userRole in _db.Set <UserRole>() on user.UserId equals userRole.UserId
                        join role in _db.Set <Role>() on userRole.RoleId equals role.RoleId
                        where user.EmailAddress == username || user.UserName == username || user.Mobile == username
                        select new { user, role };

            var _user = query.Select(c => c.user).FirstOrDefault();
            var _role = query.Select(c => c.role).FirstOrDefault();

            if (_user == null)
            {
                throw new Exception("Invalid Credentials");
            }

            var encryptedPwd    = EncryptPassword(password, _user.Salt);
            UserModel userModel = null;

            if (encryptedPwd == _user.Password)
            {
                userModel           = Core.Infrastructures.Helper.Mapper.MapUser(_user, _role); //_mapper.Map<User, UserModel>(user);//  new UserModel(user);
                _user.LastLoginDate = DateTime.Now;
                _db.Update <User>(_user);
                _db.SaveChanges();
                return(userModel);
            }
            else
            {
                throw new Exception("Invalid Credentials");
            }
        });
        public Operation <VideoModel> AddVideoOrUpdate(VideoModel model)
        {
            return(Operation.Create(() =>
            {
                if (model == null)
                {
                    _logger.LogError("Video cannot be empty");
                    throw new Exception("Video cannot be empty");
                }

                bool validate = model.Validate();
                if (!validate)
                {
                    _logger.LogError("Video ref cannot be empty");
                    throw new Exception("Video ref cannot be empty");
                }

                var query = _repo.Video.FirstOrDefault(c => c.Id == model.Id);

                if (query == null)
                {
                    query = _mapper.Map <VideoModel, Video>(model);
                    query.Points = model.Points;

                    _repo.Add <Video>(query);
                    _repo.SaveChanges();
                }
                else
                {
                    //query = _mapper.Map<VideoModel, Video>(model);

                    query.ThumbNail = model.ThumbNail ?? query.ThumbNail;
                    query.Title = model.Title == null ? query.Title : model.Title;
                    query.Views = model.Views <= 0 ? query.Views : model.Views;
                    query.Points = model.Points <= 0 ? query.Points : model.Points;
                    query.Author = model.Author ?? query.Author;

                    query.ModifiedBy = model.ModifiedBy.HasValue ? model.ModifiedBy.Value : 0;
                    query.ModifiedDate = DateTime.Now;

                    _repo.Update <Video>(query);
                    _repo.SaveChanges();
                }

                return _mapper.Map <Video, VideoModel>(query);
            }));
        }
        public Operation <QuestionModel> AddQuestionOrUpdate(QuestionModel model)
        {
            return(Operation.Create(() =>
            {
                if (model == null)
                {
                    _logger.LogError("Question cannot be empty");
                    throw new Exception("Question cannot be empty");
                }

                bool validate = model.Validate();
                if (!validate)
                {
                    _logger.LogError("Question  cannot be empty");
                    throw new Exception("Question  cannot be empty");
                }

                var query = _repo.Question.FirstOrDefault(c => c.Id == model.Id);

                if (query == null)
                {
                    query = _mapper.Map <QuestionModel, Questions>(model);
                    _repo.Add <Questions>(query);
                    _repo.SaveChanges();
                }
                else
                {
                    query.Question = model.Question;
                    query.OptionA = model.OptionA;
                    query.OptionB = model.OptionB;
                    query.OptionC = model.OptionC;
                    query.OptionD = model.OptionD;
                    query.Answer = model.Answer;

                    query.ModifiedBy = model.ModifiedBy.HasValue ? model.ModifiedBy.Value : 0;
                    query.ModifiedDate = DateTime.Now;

                    _repo.Update <Questions>(query);
                    _repo.SaveChanges();
                }

                return _mapper.Map <Questions, QuestionModel>(query);
            }));
        }
示例#4
0
        public Operation UpdateImages(string fileName, long productId)
        {
            return(Operation.Create(() =>
            {
                if (fileName == string.Empty || productId == 0)
                {
                    throw new Exception("filename cannot be empty or product not found");
                }

                var product = _repo.Product.FirstOrDefault(c => c.ProductId == productId);

                if (product != null)
                {
                    product.ImageUrl = fileName;
                    _repo.Update <Product>(product);
                    _repo.SaveChanges();
                }
            }));
        }
        public Operation <AdvertModel> AddAdvertOrUpdate(AdvertModel model)
        {
            return(Operation.Create(() =>
            {
                if (model == null)
                {
                    _logger.LogError("Advert cannot be empty");
                    throw new Exception("Advert cannot be empty");
                }

                bool validate = model.Validate();

                if (!validate)
                {
                    _logger.LogError("Advert name cannot be empty");
                    throw new Exception("Advert name cannot be empty");
                }

                var query = _repo.Advert.FirstOrDefault(c => c.AdvertId == model.AdvertId);

                if (query == null)
                {
                    query = _mapper.Map <AdvertModel, Advert>(model);
                    _repo.Add <Advert>(query);
                    _repo.SaveChanges();
                }
                else
                {
                    query.Name = model.Name;
                    query.ImageUrl = model.ImageUrl;
                    query.Description = model.Description;

                    query.ModifiedBy = model.ModifiedBy.HasValue ? model.ModifiedBy.Value : 0;
                    query.ModifiedDate = DateTime.Now;

                    _repo.Update <Advert>(query);
                    _repo.SaveChanges();
                }

                return _mapper.Map <Advert, AdvertModel>(query);
            }));
        }
示例#6
0
        public Operation UpdateCategory(CategoryModel model)
        {
            return(Operation.Create(() =>
            {
                if (model == null)
                {
                    _logger.LogError("Category cannot be empty");
                    throw new Exception("category cannot be empty");
                }

                var query = _repo.Category.FirstOrDefault(c => c.CategoryId == model.CategoryId);

                query.ContentLength = model.ContentLength;
                query.ContentType = model.ContentType;
                query.Icon = model.Icon;

                _repo.Update <Category>(query);
                _repo.SaveChanges();
            }));
        }
        public Operation <ReceiptModel> AddReceiptOrUpdate(ReceiptModel model)
        {
            return(Operation.Create(() =>
            {
                if (model == null)
                {
                    _logger.LogError("Receipt cannot be empty");
                    throw new Exception("Receipt cannot be empty");
                }

                bool validate = model.Validate();

                if (!validate)
                {
                    _logger.LogError("Receipt ref cannot be empty");
                    throw new Exception("Receipt ref cannot be empty");
                }

                var query = _repo.Receipt.FirstOrDefault(c => c.ReceiptId == model.ReceiptId);

                if (query == null)
                {
                    // Add receipt (OCR)
                    query = _mapper.Map <ReceiptModel, Receipt>(model);

                    query.CreatedBy = model.CreatedBy.Value;
                    query.ContentLength = model.ContentLength;
                    query.ContentType = model.ContentType;
                    query.FilePath = model.FilePath;
                    query.ReceiptData = model.ReceiptData;

                    _repo.Add <Receipt>(query);
                    _repo.SaveChanges();

                    //  Add user point  ..

                    decimal point = Math.Floor((decimal)(model.TotalAmount / model.PointValue));

                    // this would done manually based on instruction ..

                    //_point.AddUserPointOrUpdate(new UserPointModel()
                    //{
                    //  Channel = PointChannel.IsTotalAmount,
                    //  CreatedDate = DateTime.Now,
                    //  UserId = model.CreatedBy.Value,
                    //  Point = (long)point,

                    //});
                }
                else
                {
                    query = _mapper.Map <ReceiptModel, Receipt>(model);
                    query.ModifiedBy = model.ModifiedBy.HasValue ? model.ModifiedBy.Value : 0;
                    query.ModifiedDate = DateTime.Now;
                    query.CreatedBy = model.CreatedBy.Value;

                    query.ContentLength = model.ContentLength;
                    query.ContentType = model.ContentType;
                    query.FilePath = model.FilePath;
                    query.ReceiptData = model.ReceiptData;

                    _repo.Update <Receipt>(query);
                    _repo.SaveChanges();
                }

                return new ReceiptModel()
                {
                    Currency = query.Currency,
                    ReceiptId = query.ReceiptId,
                    MerchantName = query.MerchantName,
                    TotalAmount = query.TotalAmount,
                    ReceiptRef = query.ReceiptRef,
                    ReceiptData = query.ReceiptData
                };
                // return _mapper.Map<Receipt, ReceiptModel>(query);
            }));
        }
示例#8
0
        public Operation <UserPointModel> AddUserPointOrUpdate(UserPointModel model)
        {
            return(Operation.Create(() =>
            {
                if (model == null)
                {
                    _logger.LogError("UserPoint cannot be empty");
                    throw new Exception("UserPoint cannot be empty");
                }

                bool validate = model.Validate();
                if (!validate)
                {
                    _logger.LogError("UserPoint ref cannot be empty");
                    throw new Exception("UserPoint ref cannot be empty");
                }

                var query = _repo.UserPoint.FirstOrDefault(c => c.UserPointId == model.UserPointId);

                if (query == null)
                {
                    query = _mapper.Map <UserPointModel, UserPoint>(model);
                    query.ReceiptId = model.ReceiptId;
                    query.QuestionId = model.QuestionId;
                    query.ProductId = model.ProductId;
                    query.VideoId = model.VideoId;

                    _repo.Add <UserPoint>(query);
                    _repo.SaveChanges();
                }
                else
                {
                    //query = _mapper.Map<VideoModel, Video>(model);

                    query.UserId = model.UserId;
                    query.Channel = model.Channel;
                    query.ModifiedBy = model.ModifiedBy.HasValue ? model.ModifiedBy.Value : 0;
                    query.ModifiedDate = DateTime.Now;
                    query.Point = model.Point;

                    query.ReceiptId = model.ReceiptId;
                    query.QuestionId = model.QuestionId;
                    query.ProductId = model.ProductId;
                    query.VideoId = model.VideoId;

                    _repo.Update <UserPoint>(query);
                    _repo.SaveChanges();
                }

                return new UserPointModel()
                {
                    Channel = query.Channel,
                    IsCancelled = query.IsCancelled,
                    Point = query.Point,
                    UserId = query.UserId,
                    UserPointId = query.UserPointId,
                    Status = query.Status,
                    CreatedBy = query.CreatedBy
                };
            }));
        }