Beispiel #1
0
        public async Task <TModel> UpdateAsync(TModel model)
        {
            if (model == null)
            {
                throw new StoreException($"Missing {ModelName}");
            }

            using (Logger.BeginScope(new { identifier = model.Identifier }))
            {
                //Logger.LogDebug("UpdateAsync: {0} {1}", model.Identifier, ModelName);

                if (await ExistsAsync(model.Identifier))
                {
                    if (!model.Identifier.IsValid)
                    {
                        throw new StoreException($"{ModelName}'s Identifier is not valid");
                    }

                    await UpdateOneAsync(model.Identifier, async pair =>
                    {
                        await this.PrivilegeCheckWrite(pair.Model);

                        var set = Database.Set <TEntity>();

                        if (pair.Entity != null)
                        {
                            UpdateEntity(pair.Entity, model);
                            Database.State(pair.Entity, EntityState.Modified);
                        }
                        else
                        {
                            pair.Entity = await ToEntity(model);
                            if (typeof(TEntity) is IHaveAuditDates)
                            {
                                ((IHaveAuditDates)pair.Entity).Created = DateTime.UtcNow;
                            }

                            set.Add(pair.Entity);
                        }

                        if (typeof(TEntity) is IHaveAuditDates)
                        {
                            ((IHaveAuditDates)pair.Entity).Modified = DateTime.UtcNow;
                        }


                        return(0); // return something, else async/await gets confused
                    });

                    //Logger.LogDebug("UpdateAsync: calling GetOneAsync");
                    await CacheInvalidate(model.Identifier);

                    return(await GetOneAsync(model.Identifier));
                }
                else
                {
                    //Logger.LogDebug("UpdateAsync: calling InsertAsync");
                    return(await InsertAsync(model));
                }
            }
        }