コード例 #1
0
        public void AddOrUpdate(IUserService user, long kfid, DailyFoodUpdateModel[] models)
        {
            ExceptionHelper.ThrowIfNotId(kfid, nameof(kfid));
            ExceptionHelper.ThrowIfNull(models, nameof(models));
            var uid = user.Uid;
            var ids = models.Where(p => p.Id > 0).Select(p => p.Id).Distinct().ToArray();
            var now = DateTime.Now;
            var dailyFoodImgMaps = new List <DailyFoodImgMap>();

            Data.DailyFood[]    existEntitys;
            Data.DailyFoodImg[] existImgs;
            //using (var scope = new TransactionScope(TransactionScopeOption.Suppress))
            //{
            existEntitys = _DailyFoodRepository.Entities.Where(p => p.Uid == uid && ids.Contains(p.Id) && p.Kfid == kfid && p.IsEnabled).ToArray();
            var existDfids = existEntitys.Select(p => p.Id).ToArray();

            existImgs = _DailyFoodImgRepository.Entities.Where(p => existDfids.Contains(p.Dfid) && p.IsEnabled).ToArray();
            //    scope.Complete();
            //}
            using (var scope = new TransactionScope())
            {
                foreach (var model in models)
                {
                    var entity = existEntitys.FirstOrDefault(p => p.Id == model.Id);
                    if (entity != null)
                    {
                        if (!string.IsNullOrWhiteSpace(model.Title))
                        {
                            entity.Title = model.Title;
                        }
                        if (model.Foods != null)
                        {
                            entity.Foods = model.Foods;
                        }
                        entity.IsMain  = model.IsMain;
                        entity.EatTime = model.EatTime;
                        entity.Updated = now;
                    }
                    else
                    {
                        entity = new Data.DailyFood()
                        {
                            Kfid      = kfid,
                            Uid       = uid,
                            Title     = model.Title,
                            Foods     = model.Foods,
                            IsMain    = model.IsMain,
                            EatTime   = model.EatTime,
                            Updated   = now,
                            IsEnabled = true
                        };
                        _DailyFoodRepository.Add(entity);
                    }
                    dailyFoodImgMaps.Add(new DailyFoodImgMap(entity, model.Imgs));
                }
                _DailyFoodRepository.SaveChanges();
                AddOrUpdateImg(dailyFoodImgMaps, existImgs);
                scope.Complete();
            }
        }
コード例 #2
0
 public DailyFoodImgMap(Data.DailyFood food, FoodImgUpdateModel[] imgs)
 {
     DailyFood = food;
     Imgs      = imgs;
 }