public HttpResponseMessage Modificar(int idMaterial, [FromBody] MaterialDTO dto) { // CAD, CEN, returnValue MaterialRESTCAD materialRESTCAD = null; MaterialCEN materialCEN = null; MaterialDTOA returnValue = null; // HTTP response HttpResponseMessage response = null; string uri = null; try { SessionInitializeTransaction(); string token = ""; if (Request.Headers.Authorization != null) { token = Request.Headers.Authorization.ToString(); } int id = new UsuarioCEN().CheckToken(token); materialRESTCAD = new MaterialRESTCAD(session); materialCEN = new MaterialCEN(materialRESTCAD); // Modify materialCEN.Modificar(idMaterial, dto.Nombre , dto.Contenedor , dto.EsValido ); // Return modified object returnValue = MaterialAssembler.Convert(materialRESTCAD.ReadOIDDefault(idMaterial), session); SessionCommit(); } catch (Exception e) { SessionRollBack(); if (e.GetType() == typeof(HttpResponseException)) { throw e; } else if (e.GetType() == typeof(ReciclaUAGenNHibernate.Exceptions.ModelException) && e.Message.Equals("El token es incorrecto")) { throw new HttpResponseException(HttpStatusCode.Forbidden); } else if (e.GetType() == typeof(ReciclaUAGenNHibernate.Exceptions.ModelException) || e.GetType() == typeof(ReciclaUAGenNHibernate.Exceptions.DataLayerException)) { throw new HttpResponseException(HttpStatusCode.BadRequest); } else { throw new HttpResponseException(HttpStatusCode.InternalServerError); } } finally { SessionClose(); } // Return 404 - Not found if (returnValue == null) { return(this.Request.CreateResponse(HttpStatusCode.NotFound)); } // Return 200 - OK else { response = this.Request.CreateResponse(HttpStatusCode.OK, returnValue); return(response); } }
public async Task <IActionResult> GetProduct([FromRoute] int id) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var product = await productRepository.FindById(id); if (product == null) { return(NotFound()); } ProductDTO dto = new ProductDTO(); CategoryDTO cat_dto = new CategoryDTO(product.category); dto.ProductId = product.ProductId; dto.name = product.name; dto.description = product.description; dto.dimensions = new List <DimensionDTO>(); dto.materials = new List <MaterialDTO>(); foreach (ProductMaterial pm in product.ProductMaterials) { MaterialDTO mat_dto = new MaterialDTO(); mat_dto.name = pm.Material.name; mat_dto.description = pm.Material.description; mat_dto.MaterialId = pm.Material.MaterialId; mat_dto.finishes = new List <FinishingDTO>(); foreach (MaterialFinishing mf in pm.Material.MaterialFinishings) { FinishingDTO fdto = new FinishingDTO(); fdto.finishingId = mf.Finishing.FinishingId; fdto.description = mf.Finishing.description; fdto.name = mf.Finishing.name; mat_dto.finishes.Add(fdto); } dto.materials.Add(mat_dto); } foreach (Dimension dimension in product.dimensions) { DimensionDTO dim_dto = new DimensionDTO(); dim_dto.Depth = new MeasureDTO(); dim_dto.Height = new MeasureDTO(); dim_dto.Width = new MeasureDTO(); dim_dto.DimensionId = dimension.DimensionId; dim_dto.Depth.Id = dimension.Depth.MeasureId; dim_dto.Depth.Value = dimension.Depth.Value; dim_dto.Depth.ValueMax = dimension.Depth.ValueMax; dim_dto.Depth.isDiscrete = dimension.Depth.isDiscrete; dim_dto.Height.Id = dimension.Height.MeasureId; dim_dto.Height.Value = dimension.Height.Value; dim_dto.Height.ValueMax = dimension.Height.ValueMax; dim_dto.Height.isDiscrete = dimension.Height.isDiscrete; dim_dto.Width.Id = dimension.Width.MeasureId; dim_dto.Width.Value = dimension.Width.Value; dim_dto.Width.ValueMax = dimension.Width.ValueMax; dim_dto.Width.isDiscrete = dimension.Width.isDiscrete; dto.dimensions.Add(dim_dto); } return(Ok(dto)); }
public HttpResponseMessage CrearCP([FromBody] MaterialDTO dto) { // CAD, CEN, returnValue, returnOID MaterialRESTCAD materialRESTCAD = null; MaterialCEN materialCEN = null; MaterialDTOA returnValue = null; MaterialCP materialCP = null; int returnOID = -1; // HTTP response HttpResponseMessage response = null; string uri = null; try { SessionInitializeTransaction(); materialRESTCAD = new MaterialRESTCAD(session); materialCEN = new MaterialCEN(materialRESTCAD); materialCP = new MaterialCP(session); // Create returnOID = materialCEN.Crear(dto.Nombre, dto.Contenedor, dto.Usuario_oid); materialCP.CrearAccionMaterial(returnOID); SessionCommit(); // Convert return returnValue = MaterialAssembler.Convert(materialRESTCAD.ReadOIDDefault(returnOID), session); } catch (Exception e) { SessionRollBack(); if (e.GetType() == typeof(HttpResponseException)) { throw e; } else if (e.GetType() == typeof(ReciclaUAGenNHibernate.Exceptions.ModelException) && e.Message.Equals("El token es incorrecto")) { throw new HttpResponseException(HttpStatusCode.Forbidden); } else if (e.GetType() == typeof(ReciclaUAGenNHibernate.Exceptions.ModelException) || e.GetType() == typeof(ReciclaUAGenNHibernate.Exceptions.DataLayerException)) { throw new HttpResponseException(HttpStatusCode.BadRequest); } else { throw new HttpResponseException(HttpStatusCode.InternalServerError); } } finally { SessionClose(); } // Return 201 - Created response = this.Request.CreateResponse(HttpStatusCode.Created, returnValue); return(response); }
public HttpResponseMessage Crear([FromBody] MaterialDTO dto) { // CAD, CEN, returnValue, returnOID MaterialRESTCAD materialRESTCAD = null; MaterialCEN materialCEN = null; MaterialDTOA returnValue = null; int returnOID = -1; // HTTP response HttpResponseMessage response = null; string uri = null; try { SessionInitializeTransaction(); string token = ""; if (Request.Headers.Authorization != null) { token = Request.Headers.Authorization.ToString(); } int id = new UsuarioCEN().CheckToken(token); materialRESTCAD = new MaterialRESTCAD(session); materialCEN = new MaterialCEN(materialRESTCAD); // Create returnOID = materialCEN.Crear( //Atributo Primitivo: p_nombre dto.Nombre, //Atributo Primitivo: p_contenedor dto.Contenedor, //Atributo OID: p_usuario // attr.estaRelacionado: true dto.Usuario_oid // association role ); SessionCommit(); // Convert return returnValue = MaterialAssembler.Convert(materialRESTCAD.ReadOIDDefault(returnOID), session); } catch (Exception e) { SessionRollBack(); if (e.GetType() == typeof(HttpResponseException)) { throw e; } else if (e.GetType() == typeof(ReciclaUAGenNHibernate.Exceptions.ModelException) && e.Message.Equals("El token es incorrecto")) { throw new HttpResponseException(HttpStatusCode.Forbidden); } else if (e.GetType() == typeof(ReciclaUAGenNHibernate.Exceptions.ModelException) || e.GetType() == typeof(ReciclaUAGenNHibernate.Exceptions.DataLayerException)) { throw new HttpResponseException(HttpStatusCode.BadRequest); } else { throw new HttpResponseException(HttpStatusCode.InternalServerError); } } finally { SessionClose(); } // Return 201 - Created response = this.Request.CreateResponse(HttpStatusCode.Created, returnValue); // Location Header /* * Dictionary<string, object> routeValues = new Dictionary<string, object>(); * * // TODO: y rolPaths * routeValues.Add("id", returnOID); * * uri = Url.Link("GetOIDMaterial", routeValues); * response.Headers.Location = new Uri(uri); */ return(response); }
public long DoesMaterialExist(MaterialDTO materialDTO) { return(persistence.MaterialExists(materialDTO)); }
public EntradaMaterialCadastroViewModel() { Material = new MaterialDTO(); Classe = new ClasseDTO(); }
private void ImportMaterial(KeyValuePair <int, List <ExcelRowData> > kvp, ExcelPicture img) { //check for presence of plantName - add if not present long materialNameId = 0; int column = columnIndicesAndNames.Where(a => a.Value == "NAME").Select(b => b.Key).First(); string materialName = rowData[kvp.Key].Where(a => a.column == column).Select(b => b.data).FirstOrDefault() as string; string result = Get("DoesMaterialNameExist", "materialName=" + materialName); GetLongIdResponse r = JsonConvert.DeserializeObject <GetLongIdResponse>(result); materialNameId = r.returnedId; column = columnIndicesAndNames.Where(a => a.Value == "SIZE").Select(b => b.Key).First(); string materialSize = rowData[kvp.Key].Where(a => a.column == column).Select(b => b.data).FirstOrDefault() as string; //check for presence of plantType - add if not present long materialTypeId = 0; column = columnIndicesAndNames.Where(a => a.Value == "TYPE").Select(b => b.Key).First(); string materialType = rowData[kvp.Key].Where(a => a.column == column).Select(b => b.data).FirstOrDefault() as string; result = Get("DoesMaterialTypeExist", "materialType=" + materialType); r = JsonConvert.DeserializeObject <GetLongIdResponse>(result); materialTypeId = r.returnedId; ////using cost, retail, plantName and plant Type, check for presence of service code - add if not present //ServiceCodeDTO serviceCode = GetServiceCodeDTO(kvp); //string jsonData = JsonConvert.SerializeObject(serviceCode); //var content = new StringContent(jsonData, Encoding.UTF8, "application/json"); //result = Post("DoesServiceCodeExist", content); long serviceCodeId = 0; column = columnIndicesAndNames.Where(a => a.Value == "CODE").Select(b => b.Key).First(); string serviceCode = rowData[kvp.Key].Where(a => a.column == column).Select(b => b.data).FirstOrDefault() as string; result = Get("ServiceCodeIsNotUnique", "serviceCode=" + serviceCode); r = JsonConvert.DeserializeObject <GetLongIdResponse>(result); serviceCodeId = r.returnedId; //check for presence of plant - add if not present long materialId = 0; MaterialDTO materialDTO = GetMaterialDTO(kvp); string jsonData = JsonConvert.SerializeObject(materialDTO); StringContent content = new StringContent(jsonData, Encoding.UTF8, "application/json"); result = Post("DoesMaterialExist", content); r = JsonConvert.DeserializeObject <GetLongIdResponse>(result); materialId = r.returnedId; column = columnIndicesAndNames.Where(a => a.Value == "SKU").Select(b => b.Key).FirstOrDefault(); string sku = String.Empty; if (column == 9) { sku = rowData[kvp.Key].Where(a => a.column == column).Select(b => b.data).FirstOrDefault() as string; } //check for presence of inventory - add if not present ImportMaterialRequest request = new ImportMaterialRequest(); //add image if present if (img != null) { ImageConverter imgCon = new ImageConverter(); request.imageBytes = (byte[])imgCon.ConvertTo(img.Image, typeof(byte[])); } //create DTO - send to service - backend will do the hookup request.MaterialSize = materialSize; request.AddMaterialRequest.Material.MaterialSize = materialSize; if (materialNameId == 0) { request.MaterialName = materialName; } else { request.AddMaterialRequest.Material.MaterialNameId = materialNameId; } if (materialTypeId == 0) { request.MaterialType = materialType; } else { request.AddMaterialRequest.Material.MaterialTypeId = materialTypeId; } request.ServiceCode = GetServiceCodeDTO(kvp); if (serviceCodeId > 0) { ServiceCodeDTO original = GetServiceCodeById(serviceCodeId); UpdateServiceCode(original, request.ServiceCode); request.ServiceCode = original; request.ServiceCode.ServiceCodeId = serviceCodeId; request.AddMaterialRequest.Inventory.ServiceCodeId = serviceCodeId; } if (!String.IsNullOrEmpty(sku)) { //Currently, no db support //request.ServiceCode.SKU = sku; } jsonData = JsonConvert.SerializeObject(request); content = new StringContent(jsonData, Encoding.UTF8, "application/json"); Post("ImportMaterial", content); }
private MaterialDTO GetMaterialDTO(KeyValuePair <int, List <ExcelRowData> > kvp) { MaterialDTO dto = new MaterialDTO(); return(dto); }
public ActionResult EditProduct(ProductVM model, HttpPostedFileBase file = null) { int id = model.Id; using (Db db = new Db()) { model.Categories = new SelectList(db.Categories.ToList(), "Id", "Name"); model.Materials = new SelectList(db.Materials.ToList(), "Id", "Name"); } if (file != null && file.ContentLength > 0) { string ext = file.ContentType.ToLower(); if (ext != "image/jpg" && ext != "image/jpeg" && ext != "image/pjpeg" && ext != "image/gif" && ext != "image/x-png" && ext != "image/png") { using (Db db = new Db()) { ModelState.AddModelError("", "Формат файла неподдерживается. Доступные форматы: jpg,jpeg,pjpeg,gif,x-png,png"); return(View(model)); } } } if (!ModelState.IsValid) { return(View(model)); } using (Db db = new Db()) { if (db.Products.Where(x => x.Id != id).Any(x => x.Name == model.Name)) { ModelState.AddModelError("", "Имя уже существует"); } } using (Db db = new Db()) { ProductDTO dto = db.Products.Find(id); dto.Name = model.Name; dto.Slug = model.Name.Replace(" ", "-").ToLower(); dto.Description = model.Description; dto.Price = model.Price; dto.Discount = model.Discount; dto.CategoryId = model.CategoryId; dto.MaterialId = model.MaterialId; dto.SectionId = model.SectionId; dto.SectionName = model.SectionName; if (file != null) { dto.ImgType = file.ContentType; dto.Img = new byte[file.ContentLength]; file.InputStream.Read(dto.Img, 0, file.ContentLength); } CategoryDTO catDTo = db.Categories.FirstOrDefault(x => x.Id == model.CategoryId); dto.CategoryName = catDTo.Name; MaterialDTO matDTO = db.Materials.FirstOrDefault(x => x.Id == model.MaterialId); dto.MaterialName = matDTO.Name; SectionDTO secDTO = db.Sections.FirstOrDefault(x => x.Id == model.SectionId); dto.SectionName = secDTO.Name; db.SaveChanges(); id = db.Products.Max(i => i.Id); string prodCode = model.SectionId.ToString("00") + model.CategoryId.ToString("00") + id.ToString("0000"); ProductDTO dto_ = db.Products.Find(id); dto_.ProductCode = prodCode; db.SaveChanges(); } TempData["SM"] = "Продукт изменен!"; return(RedirectToAction("Products")); }