/// <summary> /// Delete an entity. /// </summary> /// <param name="model"></param> public void Delete(AssetViewModel model) { var entity = model.ToEntity(); this._AssetsRepository.Delete(entity); #region Commit Changes this._unitOfWork.Commit(); #endregion }
/// <summary> /// Add an entity. /// </summary> /// <param name="model"></param> /// <returns></returns> public AssetViewModel Add(AssetViewModel model) { this.ThrowExceptionIfExist(model); //LocationViewModel loc = null; Location location = new Location(); if (model.Location == null && (model.LocationId == null || model.LocationId == 0)) { throw new GeneralException(15); } else if ( (model.LocationId == null || model.LocationId == 0) && model.Location != null) { model.Location.Date = DateTime.Now; ConditionFilter <Location, long> condition = new ConditionFilter <Location, long> { Query = (e => e.Code == model.Location.Code && e.Id != model.Id) }; var existEntity = this._locationsRepository.Get(condition).FirstOrDefault(); if (existEntity != null) { throw new ItemAlreadyExistException((int)ErrorCodeEnum.CodeAlreadyExist); } location = model.Location.ToEntity(); Location locationAr = new Location { ParentKeyLocationId = location.Id, Title = model.Location.TitleAr, Description = model.Location.DescriptionAr, Language = Language.Arabic }; Location locationEn = new Location { ParentKeyLocationId = location.Id, Title = model.Location.TitleEn, Description = model.DescriptionEn, Language = Language.English }; location = this._locationsRepository.Add(location); location.ChildTranslatedLocations.Add(locationAr); this._locationsRepository.Add(locationAr); location.ChildTranslatedLocations.Add(locationEn); this._locationsRepository.Add(locationEn); //loc = location.ToModel(); } var entity = model.ToEntity(); entity.BarCode = Guid.NewGuid().ToString(); entity.CurrentValue = entity.Amount; AssetLocation assetLocation = new AssetLocation() { Date = DateTime.Now }; if (model.LocationId != null) { assetLocation.LocationId = model.LocationId; // entity.LocationId = model.LocationId; // entity.Location = null; } else { assetLocation.Location = location; // entity.Location = location; } entity.AssetLocations.Add(assetLocation); entity = this._AssetsRepository.Add(entity); this._assetLocationsRepository.Add(assetLocation); #region Add AssetLocation Translation var assetLocationAr = new AssetLocation { Id = entity.Id, Description = model.DescriptionAr, Language = Language.Arabic }; assetLocation.ChildTranslatedAssetLocations.Add(assetLocationAr); this._assetLocationsRepository.Add(assetLocationAr); var assetLocationEn = new AssetLocation { Id = entity.Id, Description = model.DescriptionEn, Language = Language.English }; assetLocation.ChildTranslatedAssetLocations.Add(assetLocationEn); this._assetLocationsRepository.Add(assetLocationEn); #endregion var entityAr = new Asset { Id = entity.Id, Description = model.DescriptionAr, //Name = model.NameAr, Language = Language.Arabic }; entity.ChildTranslatedAssets.Add(entityAr); this._AssetsRepository.Add(entityAr); var entityEn = new Asset { Id = entity.Id, Description = model.DescriptionEn, //Name = model.NameEn, Language = Language.English }; entity.ChildTranslatedAssets.Add(entityEn); this._AssetsRepository.Add(entityEn); #region Commit Changes this._unitOfWork.Commit(); #endregion if (model.CostCenterId.HasValue) { var objectCostCenter = new ObjectCostCenter { CostCenterId = model.CostCenterId.Value, ObjectId = entity.Id, ObjectType = ObjectType.Asset }; this._objectCostCentersRepository.Add(objectCostCenter); #region Commit Changes this._unitOfWork.Commit(); #endregion } model = entity.ToModel(); return(model); }