Example #1
0
            public async Task <Response> Handle(Request request)
            {
                var entity = await _context.AthleteWeights
                             .Include(x => x.Tenant)
                             .Include(x => x.Athlete)
                             .SingleOrDefaultAsync(x => x.Id == request.AthleteWeight.Id &&
                                                   x.Tenant.UniqueId == request.TenantUniqueId);

                if (entity == null)
                {
                    var tenant = await _context.Tenants.SingleAsync(x => x.UniqueId == request.TenantUniqueId);

                    var athlete = await _context.Athletes.SingleAsync(x => x.Username == request.Username);

                    _context.AthleteWeights.Add(entity = new AthleteWeight()
                    {
                        TenantId = tenant.Id, AthleteId = athlete.Id
                    });
                }

                entity.WeightInKgs = request.AthleteWeight.WeightInKgs;

                entity.WeighedOn = request.AthleteWeight.WeighedOn;

                await _context.SaveChangesAsync();

                _bus.Publish(new AddedOrUpdatedAthleteWeightMessage(AthleteWeightApiModel.FromAthleteWeight(entity), request.CorrelationId, request.TenantUniqueId));

                return(new Response());
            }
        public static TModel FromAthleteWeight <TModel>(AthleteWeight athleteWeight) where
        TModel : AthleteWeightApiModel, new()
        {
            var model = new TModel();

            model.Id          = athleteWeight.Id;
            model.TenantId    = athleteWeight.TenantId;
            model.WeightInKgs = athleteWeight.WeightInKgs;
            model.WeighedOn   = athleteWeight.WeighedOn;

            return(model);
        }
 public static AthleteWeightApiModel FromAthleteWeight(AthleteWeight athleteWeight)
 => FromAthleteWeight <AthleteWeightApiModel>(athleteWeight);