public HttpResponseMessage Create(HttpRequestMessage request, ProductModel ProductVm) { return(CreateHttpResponse(request, () => { HttpResponseMessage response = null; if (!ModelState.IsValid) { response = request.CreateResponse(HttpStatusCode.BadRequest, ModelState); } else { // Create tag mới var TagsCreate = ProductVm.Tags.Where(x => x.isNew == true).ToList(); var ListTags = Mapper.Map <List <TagModel>, List <Tag> >(TagsCreate); var ListTagCreate = _Tag.AddTags(ListTags); /// Tạo product var modelVm = Mapper.Map <ProductModel, Product>(ProductVm); _Product.Create(modelVm); _Product.Save(); /// Tag tag List <ProductTag> ListProductTag = new List <ProductTag>(); foreach (var Tag in ProductVm.Tags.Where(x => x.isNew == false).ToList()) { ProductTag ProductTag = new ProductTag(); ProductTag.ProductID = modelVm.ProductID; ProductTag.TagID = Convert.ToInt32(Tag.id); ListProductTag.Add(ProductTag); } foreach (var Tag in ListTagCreate) { ProductTag ProductTag = new ProductTag(); ProductTag.ProductID = modelVm.ProductID; ProductTag.TagID = Tag.TagID; ListProductTag.Add(ProductTag); } _ProductTagReporistory.CreateProductTag(ListProductTag); _ProductTagReporistory.Save(); var responseData = Mapper.Map <Product, ProductModel>(modelVm); response = request.CreateResponse(HttpStatusCode.Created, responseData); } return response; })); }