Пример #1
0
        public override void AddOrUpdate(ClientAPI model)
        {
            var repository = new RepositoryFactory().CreateGenericRepository <ClientAPI>();

            Task.Run(async() => {
                var oldValue = await repository.GetFirstAsync(x => x.ID == model.ID);
                if (oldValue != null)
                {
                    await repository.UpdateAsync(oldValue, model);

                    var detailRepo = new RepositoryFactory().CreateGenericRepository <APIParameter>();
                    var oldDetails = await detailRepo.GetListAsync(x => x.ApiID == model.ID);

                    if (oldDetails.Count() > 0)
                    {
                        //delete records from the db
                        await detailRepo.ConcreteDeleteRangeAsync(oldDetails);
                    }

                    foreach (var param in model.Params)
                    {
                        //manually assing values of fk here
                        param.ApiID = model.ID;
                    }

                    //adding details as new records in db
                    await detailRepo.AddRangeAsync(model.Params);
                }
                else
                {
                    model.RecordStatus = BaseEntity.RecordStatusType.Active;
                    await repository.AddAsync(model);
                }
            }).Wait();
        }