public async Task <IActionResult> CreateCatalogAssociation([FromBody] CatalogAssociateViewModel item) { if (item == null) { return(this.BadRequest()); } foreach (var catalogItem in item.CatalogItems) { await this.unitOfWork.CatalogAssociationRepository.AddAsync( new CatalogAssociation { ParentCatalogItemId = item.ParentCatalogItemId, ChildCatalogItemId = catalogItem.Id }); } await this.unitOfWork.SaveAsync(); return(this.CreatedAtRoute("GetCatalogItem", new { id = 1, itemId = 1 }, item)); }
public async Task <IActionResult> EditCatalogAssociation( int parentCatalogItemId, int childCatalogId, [FromBody] CatalogAssociateViewModel item) { if (item == null) { return(this.BadRequest()); } var catalogsAssociation = await this.GatalogAssociateModel(parentCatalogItemId, childCatalogId); if (catalogsAssociation == null) { return(this.NotFound()); } var currentValues = await this.unitOfWork.CatalogAssociationRepository.GetQuery().Where( x => x.ParentCatalogItemId == parentCatalogItemId && x.ChildCatalogItem.CatalogId == childCatalogId).ToListAsync(); var newValues = item.CatalogItems.Select(x => new CatalogAssociation { ChildCatalogItemId = x.Id, ParentCatalogItemId = parentCatalogItemId }); this.unitOfWork.CatalogAssociationRepository.TryUpdateManyToMany( currentValues, newValues, x => $"{x.ParentCatalogItemId}-{x.ChildCatalogItemId}"); await this.unitOfWork.SaveAsync(); return(new NoContentResult()); }