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); })); }
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); })); }
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 <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 async Task AddClaims(string userId, ClaimModel[] models) { long u = long.Parse(userId); var claimQuery = from userClaim in _db.Set <Claim>() where userClaim.UserId == u select userClaim; var userClaims = await claimQuery.ToListAsync(); //Get Existing Claims var existingQueries = from claim in userClaims join model in models on claim.Type equals model.Type select new { claim, model }; //Update Existing Claims foreach (var record in existingQueries) { record.claim.Value = record.model.Value; } //Add new Claims var newClaims = models.Except(existingQueries.Select(r => r.model)); foreach (var newClaim in newClaims) { _db.Add(new Claim { UserId = long.Parse(userId), Type = newClaim.Type, Value = newClaim.Value }); } _db.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); })); }
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 }; })); }