Beispiel #1
0
        public async Task <FeatureViewModel> CreateFeature([FromBody] FeatureViewModel featureVM)
        {
            var domainModel = featureVM.ToDomainModel();

            if (featureVM.FeatureId > 0) // editing an existing feature
            {
                //make sure they're the author of this feature
                var existingFeature = await this.dbCtx.Features.FirstOrDefaultAsync(f => f.Player.Id == userSession.Player.Id && f.Id == featureVM.FeatureId);

                if (existingFeature != null)
                {
                    //yep, it's theirs
                    existingFeature.Delisted = true;
                }
            }

            domainModel.Id = 0;

            domainModel.RequiredFlaws.ForEach(f => dbCtx.Attach(f));
            domainModel.RequiredOaths.ForEach(o => dbCtx.Attach(o));

            dbCtx.Attach(userSession.Player);
            domainModel.Player      = userSession.Player;
            domainModel.CreatedAtMS = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();

            dbCtx.Features.Add(domainModel);


            await dbCtx.SaveChangesAsync();

            domainModel.Player = userSession.Player;

            return(domainModel.ToViewModel());
        }