Пример #1
0
            public async Task <AddOrUpdateProfileResponse> Handle(AddOrUpdateProfileRequest request)
            {
                var entity = await _context.Profiles
                             .Include(x => x.WeightSnapShots)
                             .SingleOrDefaultAsync(x => x.Id == request.Profile.Id && x.TenantId == request.TenantId);

                if (entity == null)
                {
                    _context.Profiles.Add(entity = new Profile());
                }
                entity.Name     = request.Profile.Name;
                entity.TenantId = request.TenantId;

                entity.WeightSnapShots.Clear();

                foreach (var weightSnapShot in request.Profile.WeightSnapShots)
                {
                    var wss = await _context.WeightSnapShots.Include(x => x.Profile).Where(x => x.Id == weightSnapShot.Id).SingleOrDefaultAsync();

                    if (wss == null)
                    {
                        wss = new WeightSnapShot();
                    }
                    wss.Id        = weightSnapShot.Id;
                    wss.Pounds    = weightSnapShot.Pounds;
                    wss.WeighedOn = weightSnapShot.WeighedOn;

                    entity.WeightSnapShots.Add(wss);
                }

                await _context.SaveChangesAsync();

                return(new AddOrUpdateProfileResponse());
            }
        public static TModel FromWeightSnapShot <TModel>(WeightSnapShot weightSnapShot) where
        TModel : WeightSnapShotApiModel, new()
        {
            var model = new TModel();

            model.Id        = weightSnapShot.Id;
            model.TenantId  = weightSnapShot.TenantId;
            model.Pounds    = weightSnapShot.Pounds;
            model.WeighedOn = weightSnapShot.WeighedOn;
            return(model);
        }
 public static WeightSnapShotApiModel FromWeightSnapShot(WeightSnapShot weightSnapShot)
 => FromWeightSnapShot <WeightSnapShotApiModel>(weightSnapShot);