//DONE public ActionResult PostCategory(CreateCategoryVM model) { try { if (ModelState.IsValid) { var categoryToCreate = _mapper.Map <Category>(model); var result = _categoryServices.AddCategory(categoryToCreate); if (!result) { return(StatusCode(StatusCodes.Status500InternalServerError, "Algo salio mal, contacta el Administrador")); } return(Json("La categoría ha sido agregada.")); } return(BadRequest(FormatedModelStateErrors.GetErrorsFormated(ModelState))); } catch (Exception ex) { //TODO: Log the exception return(StatusCode(StatusCodes.Status500InternalServerError, "Algo salio mal, contacta el Administrador")); } }
//DONE public ActionResult PostClient(CreateClientVM model) { try { if (ModelState.IsValid) { if (_clientServices.Exists(model.Document)) { return(BadRequest($"Existe un cliente con el documento {model.Document} registrado.")); } var clientToCreate = _mapper.Map <Client>(model); clientToCreate.CreationDate = DateTime.Now; var result = _clientServices.AddClient(clientToCreate); if (!result) { return(StatusCode(StatusCodes.Status500InternalServerError, "Algo salio mal tratando de agregar el cliente, Intente de nuevo o contacta el Administrador.")); } return(Json("El cliente ha sido agregado.")); } return(BadRequest(FormatedModelStateErrors.GetErrorsFormated(ModelState))); } catch (Exception ex) { //TODO: Log the exception return(StatusCode(StatusCodes.Status500InternalServerError, "Algo salio mal tratando de agregar el cliente, Intente de nuevo o contacta el Administrador.")); } }
//DONE public ActionResult EditCategory(CategoryVM model) { try { if (ModelState.IsValid) { var categoryToEdit = _categoryServices.GetCategoryById(model.ID); if (categoryToEdit == null) { return(BadRequest("La categoría que tratas de actualizar no existe!")); } categoryToEdit.Name = model.Name; var result = _categoryServices.UpdateCategory(categoryToEdit); if (!result) { return(StatusCode(StatusCodes.Status500InternalServerError, "Algo salio mal trantando de actualizar la categoría, contacta el Administrador")); } return(Json("Categoría actualizada con exito.!")); } return(BadRequest(FormatedModelStateErrors.GetErrorsFormated(ModelState))); } catch (Exception ex) { //TODO: Log the exception return(StatusCode(StatusCodes.Status500InternalServerError, "Algo salio mal, contacta el Administrador")); } }
//DONE public async Task <ActionResult> PostRole(CreateRoleVM model) { try { if (ModelState.IsValid) { if (await _roleManager.RoleExistsAsync(model.Name)) { return(BadRequest("Este rol ya ha sido creado anteriomente")); } var newRole = new IdentityRole { Name = model.Name }; var result = await _roleManager.CreateAsync(newRole); if (!result.Succeeded) { return(StatusCode(StatusCodes.Status500InternalServerError, "Algo salio mal trantando de crear el rol. Intenta de nuevo o contacta al adminitrador del sistema")); } return(Json("El rol ha sido creado")); } return(BadRequest(FormatedModelStateErrors.GetErrorsFormated(ModelState))); } catch (Exception ex) { //TODO: Log the exception return(StatusCode(StatusCodes.Status500InternalServerError, "Algo salio mal trantando de crear el rol. Intenta de nuevo o contacta al adminitrador del sistema")); } }
//DONE public ActionResult PostBrand(CreateBrandVM model) { try { if (ModelState.IsValid) { var bandToCreate = _mapper.Map <Brand>(model); var result = _brandServices.AddBrand(bandToCreate); if (!result) { return(StatusCode(StatusCodes.Status500InternalServerError, "Algo salio mal tratando de crear la marca, Intente de nuevo o contacta el Administrador")); } return(Json("La marca ha sido agregada.")); } return(BadRequest(FormatedModelStateErrors.GetErrorsFormated(ModelState))); } catch (Exception ex) { //TODO: Log the exception return(StatusCode(StatusCodes.Status500InternalServerError, "Algo salio mal, contacta el Administrador")); } }
//DONE public ActionResult EditProduct(UpdateProductVM model) { try { if (ModelState.IsValid) { var productToEdit = _productServices.GetPrductById(model.ID); if (productToEdit == null) { return(BadRequest("El producto que trata de actualizar no existe!")); } if (model.BuyPrice > model.SellPrice) { return(BadRequest("El precio de venta tiene que ser mayor al de compra")); } productToEdit.Updated = true; //Actualizo el producto actual en la base de datos. _productServices.UpdateProduct(productToEdit); //Creo el nuevo producto var product = new Product() { Code = productToEdit.Code, Description = model.Description, CategoryID = model.CategoryID, BrandID = model.BrandID, BuyPrice = model.BuyPrice, SellPrice = model.SellPrice, CreationDate = productToEdit.CreationDate, ModificationDate = DateTime.Now, Status = model.Status, Updated = false, Deleted = false }; _productServices.AddProduct(product); var result = _productServices.Save(); if (!result) { return(StatusCode(StatusCodes.Status500InternalServerError, "Algo salio mal, contacta el Administrador")); } return(Json("Producto actualizado con exito.!")); } return(BadRequest(FormatedModelStateErrors.GetErrorsFormated(ModelState))); } catch (Exception ex) { //TODO: Log the exception return(StatusCode(StatusCodes.Status500InternalServerError, "Algo salio mal, contacta el Administrador")); } }
//DONE public ActionResult PostProduct(CreateProductVM model) { try { if (ModelState.IsValid) { if (!_productServices.Exists(model.Description)) { return(BadRequest("Este producto ha sido creado anteriormente")); } if (model.BuyPrice > model.SellPrice) { return(BadRequest("El precio de venta de ser mayor al de compra")); } model.CreationDate = DateTime.Now; var productToCreate = _mapper.Map <Product>(model); productToCreate.Status = true; _productServices.AddProduct(productToCreate); var result = _productServices.Save(); if (!result) { return(StatusCode(StatusCodes.Status500InternalServerError, "Algo salio mal, contacta el Administrador")); } return(Json("El producto ha sido agregado")); } return(BadRequest(FormatedModelStateErrors.GetErrorsFormated(ModelState))); } catch (Exception ex) { //TODO: Log the exception return(StatusCode(StatusCodes.Status500InternalServerError, "Algo salio mal, contacta el Administrador")); } }
//DONE public ActionResult EditClient(UpdateClienteVM model) { try { if (ModelState.IsValid) { var clientToEdit = _clientServices.GetClientById(model.ID); if (model.Document != clientToEdit.Document) { if (_clientServices.Exists(model.Document)) { return(BadRequest($"Existe un cliente con el documento {model.Document} registrado.")); } } clientToEdit.FirstName = model.FirstName; clientToEdit.LastName = model.LastName; clientToEdit.Document = model.Document; clientToEdit.Email = model.Email; clientToEdit.Phone = model.Phone; clientToEdit.Address = model.Address; var result = _clientServices.UpdateClient(clientToEdit); if (!result) { return(StatusCode(StatusCodes.Status500InternalServerError, "Algo salio mal tratando de actualizar el cliente, Intente de nuevo o contacta el Administrador.")); } return(Json("El cliente ha sido actualizado.")); } return(BadRequest(FormatedModelStateErrors.GetErrorsFormated(ModelState))); } catch (Exception ex) { //TODO: Log the exception return(StatusCode(StatusCodes.Status500InternalServerError, "Algo salio mal tratando de actualizar el cliente, Intente de nuevo o contacta el Administrador.")); } }
//DONE public async Task <ActionResult> PostUser(CreateUserVM model) { try { //TODO: HACER ESTE PROCESO EN EL REPOSITORIO CON SP if (ModelState.IsValid) { var newUser = new User { FirstName = model.FirstName, LastName = model.LastName, Status = true, }; newUser.UserName = await GetGeneratedUserName(); var result = await _userManager.CreateAsync(newUser, model.Password); if (!result.Succeeded) { return(StatusCode(StatusCodes.Status500InternalServerError, "Algo salio mal trantando de crear el usuario. Intenta de nuevo o contacta al adminitrador del sistema")); } await _userManager.AddToRoleAsync(newUser, model.Role); return(Json("El usuario ha sido creado")); } return(BadRequest(FormatedModelStateErrors.GetErrorsFormated(ModelState))); } catch (Exception ex) { //TODO: Log the exception return(StatusCode(StatusCodes.Status500InternalServerError, "Algo salio mal trantando de crear el usuario. Intenta de nuevo o contacta al adminitrador del sistema")); } }
//DONE public async Task <ActionResult> EditUser(UpdateUserMV model) { try { if (ModelState.IsValid) { var userToEdit = await _userManager.FindByIdAsync(model.Id); if (userToEdit == null) { return(BadRequest("El usuario que tratas de editar no existe.")); } userToEdit.FirstName = model.FirstName; userToEdit.LastName = model.LastName; userToEdit.Status = model.Status; var result = await _userManager.UpdateAsync(userToEdit); if (!result.Succeeded) { return(StatusCode(StatusCodes.Status500InternalServerError, "Algo salio mal trantando de actualizar el usuario. Intenta de nuevo o contacta al adminitrador del sistema")); } return(Json("El usuario ha sido creado")); } return(BadRequest(FormatedModelStateErrors.GetErrorsFormated(ModelState))); } catch (Exception ex) { //TODO: Log the exception return(StatusCode(StatusCodes.Status500InternalServerError, "Algo salio mal trantando de actualizar el usuario. Intenta de nuevo o contacta al adminitrador del sistema")); } }