// PUT api/Data/5
        public HttpResponseMessage PutEmployee(Int32 id, Employee employee)
        {
            if (!ModelState.IsValid)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }

            if (id != employee.Id)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }

            db.Entry(employee).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex));
            }

            return(Request.CreateResponse(HttpStatusCode.OK));
        }
Esempio n. 2
0
        public IHttpActionResult PutUser(int id, User user)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != user.User_ID)
            {
                return(BadRequest());
            }

            db.Entry(user).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!UserExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Esempio n. 3
0
        public static Sale DeleteSale(int saleID)
        {
            using (var context = new ServicesContext())
            {
                var selectedSale = context.Sales.FirstOrDefault(b => b.ID == saleID);

                // Checking if the category exists within the database.
                if (selectedSale != null)
                {
                    // Minus one from the counter of the daily and monthly sale stats.
                    var selectedDailyStat   = context.dailyStats.FirstOrDefault(b => b.salesDate == selectedSale.saleDate);
                    var selectedMonthlyStat = context.monthlyStats.FirstOrDefault
                                                  (b => b.salesMonth == selectedSale.saleDate.Month && b.salesYear == selectedSale.saleDate.Year);

                    if (selectedDailyStat != null)
                    {
                        selectedDailyStat.productsSold--;
                    }
                    if (selectedMonthlyStat != null)
                    {
                        selectedMonthlyStat.productsSold--;
                    }

                    context.Sales.Remove(selectedSale);
                    context.SaveChanges();

                    return(selectedSale);
                }
                // If it does not, return null.
                else
                {
                    return(null);
                }
            }
        }
        public ActionResult DeleteUser(int?id)
        {
            if (id == null)
            {
                return(HttpNotFound());
            }

            User u = db.Users.Find(id);

            if (u != null)
            {
                db.Users.Remove(u);
                db.SaveChanges();
            }
            return(Redirect("~/Account/DeleteUser"));
        }
Esempio n. 5
0
        public static JArray EliminarFactura(int facturaID)
        {
            JArray arrayJSON = BuscarFactura(facturaID);

            using (var context = new ServicesContext())
            {
                var factura = context.facturas.Where(x => x.ID == facturaID).
                              Select(x => new { x.ID, x.fecha, x.clienteFK }).FirstOrDefault();

                // La factura no existe, retornar nulo.
                if (factura == null)
                {
                    return(null);
                }
                else
                {
                    var encontrarFactura = context.facturas.SingleOrDefault(x => x.ID == facturaID);
                    context.facturas.Remove(encontrarFactura);
                    ServiciosDetallesFactura.RemoverDetallesFactura(facturaID);

                    context.SaveChanges();
                    return(arrayJSON);
                }
            }
        }
Esempio n. 6
0
        public ActionResult AddComment(int?serviceId)
        {
            if (serviceId == null)
            {
                return(HttpNotFound());
            }
            Comment comment = new Comment();

            comment.Text      = Request.Form["commentText"];
            comment.ServiceId = (int)serviceId;
            comment.SenderId  = ((User)Session["User"]).Id;

            db.Comments.Add(comment);
            db.SaveChanges();
            return(Redirect("ServiceInfo/" + serviceId));
        }
        public static Product CreateProduct(Product product)
        {
            using (var context = new ServicesContext())
            {
                // Check if product already exists within database.
                var findExistingProduct = context.Products.FirstOrDefault
                                              (b => b.productName.ToUpper() == product.productName.ToUpper());

                // Product object corrections
                product.productName        = product.productName.Trim();
                product.productDescription = product.productDescription.Trim();

                // Product doesn't exist.
                if (findExistingProduct == null)
                {
                    // Sum one to the counter of the product's category
                    var category = context.Categories.Single(b => b.ID == product.categoryID);
                    category.ammountProducts++;

                    context.Products.Add(product);
                    context.SaveChanges();

                    var newestProduct = context.Products.ToArray().Last();
                    return(newestProduct);
                }
                // Product already exists
                else
                {
                    return(null);
                }
            }
        }
        public static Product DeleteProduct(int productID)
        {
            using (var context = new ServicesContext())
            {
                var selectedProduct = context.Products.FirstOrDefault(b => b.ID == productID);

                // Checking if the category exists within the database.
                if (selectedProduct != null)
                {
                    // Minus one from the counter of the product's category.
                    var selectedCategory = context.Categories.FirstOrDefault(b => b.ID == selectedProduct.categoryID);
                    selectedCategory.ammountProducts--;

                    context.Products.Remove(selectedProduct);
                    context.SaveChanges();

                    return(selectedProduct);
                }
                // If it does not, return null.
                else
                {
                    return(null);
                }
            }
        }
        public static Product UpdateProduct(int productID, Product product)
        {
            using (var context = new ServicesContext())
            {
                var selectedProduct = context.Products.FirstOrDefault(b => b.ID == productID);

                // Checking if the product does exist within the database.
                if (selectedProduct != null)
                {
                    // Update category counters if a new category has been set for the product.
                    if (selectedProduct.categoryID != product.categoryID)
                    {
                        var oldCategory = context.Categories.Single(b => b.ID == selectedProduct.categoryID);
                        oldCategory.ammountProducts--;
                        var newCategory = context.Categories.Single(b => b.ID == product.categoryID);
                        newCategory.ammountProducts++;
                    }

                    selectedProduct.categoryID         = product.categoryID;
                    selectedProduct.productName        = product.productName.Trim();
                    selectedProduct.productDescription = product.productDescription.Trim();
                    selectedProduct.productAmmount     = product.productAmmount;
                    selectedProduct.productPrice       = product.productPrice;
                    context.SaveChanges();

                    return(selectedProduct);
                }
                // If it does not, return null.
                else
                {
                    return(null);
                }
            }
        }
        public static DailyStats DeleteDailyStat(int statID)
        {
            using (var context = new ServicesContext())
            {
                var selectedDailyStat = context.dailyStats.FirstOrDefault(b => b.ID == statID);

                // Checking if the daily stats exists within the database.
                if (selectedDailyStat != null)
                {
                    // Remove daily sales from the monthly stats counter.
                    var selectedMonthlyStat = context.monthlyStats.FirstOrDefault
                                                  (b => b.salesMonth == selectedDailyStat.salesDate.Month &&
                                                  b.salesYear == selectedDailyStat.salesDate.Year);

                    if (selectedMonthlyStat != null)
                    {
                        selectedMonthlyStat.productsSold -= selectedDailyStat.productsSold;
                    }

                    context.dailyStats.Remove(selectedDailyStat);
                    context.SaveChanges();

                    return(selectedDailyStat);
                }
                // If it does not, return null.
                else
                {
                    return(null);
                }
            }
        }
Esempio n. 11
0
        public static Category CreateCategory(Category category)
        {
            using (var context = new ServicesContext())
            {
                // Check if category already exists within database.
                var findExistingCategory = context.Categories.FirstOrDefault
                                               (b => b.categoryName.ToUpper() == category.categoryName.ToUpper());

                // Category object corrections
                category.categoryName = category.categoryName.Trim();

                // Category doesn't exist.
                if (findExistingCategory == null)
                {
                    context.Categories.Add(category);
                    context.SaveChanges();

                    var newestCategory = context.Categories.ToArray().Last();
                    return(newestCategory);
                }
                // Category already exists
                else
                {
                    return(null);
                }
            }
        }
 public static Cliente EliminarCliente(int numeroRUT)
 {
     using (var context = new ServicesContext())
     {
         var encontrarCliente = context.clientes.SingleOrDefault(x => x.RUT == numeroRUT);
         context.clientes.Remove(encontrarCliente);
         context.SaveChanges();
         return(encontrarCliente);
     }
 }
Esempio n. 13
0
        public static Sale CreateSale(Sale sale)
        {
            using (var context = new ServicesContext())
            {
                var currentDate = DateTime.Now.Date;
                // Check if there's already a sales stat for this day.
                var findDailyStat = context.dailyStats.FirstOrDefault
                                        (b => b.salesDate == currentDate);

                var findMonthlyStat = context.monthlyStats.FirstOrDefault
                                          (b => b.salesMonth == DateTime.Now.Month && b.salesYear == DateTime.Now.Year);

                // If there is an existing daily stat, sum one to the counter.
                if (findDailyStat != null)
                {
                    findDailyStat.productsSold++;
                }
                // If there isn't, create a new stat for this day.
                else
                {
                    DailyStats newStats = new DailyStats();
                    newStats.productsSold++;
                    context.dailyStats.Add(newStats);
                }

                // If the monthly stat already exists, sum one to the counter of that month.
                if (findMonthlyStat != null)
                {
                    findMonthlyStat.productsSold++;
                }
                // If there isn't an existing monthly stat, create one.
                else
                {
                    MonthlyStats newStats = new MonthlyStats();
                    newStats.productsSold++;
                    context.monthlyStats.Add(newStats);
                }


                // Sale object corrections
                sale.saleDescription = sale.saleDescription.Trim();

                context.Sales.Add(sale);
                context.SaveChanges();
                return(sale);
            }
        }
 public static Producto EliminarProducto(int productoID)
 {
     using (var context = new ServicesContext())
     {
         var encontrarProducto = context.productos.SingleOrDefault(x => x.ID == productoID);
         if (encontrarProducto == null)
         {
             return(null);
         }
         else
         {
             context.productos.Remove(encontrarProducto);
             context.SaveChanges();
             return(encontrarProducto);
         }
     }
 }
 public static Producto EditarProducto(int productoID, Producto producto)
 {
     using (var context = new ServicesContext())
     {
         var encontrarProducto = context.productos.SingleOrDefault(x => x.ID == productoID);
         if (encontrarProducto == null)
         {
             return(null);
         }
         else
         {
             encontrarProducto.nombreProducto = producto.nombreProducto.Trim();
             encontrarProducto.precioProducto = producto.precioProducto;
             context.SaveChanges();
             return(encontrarProducto);
         }
     }
 }
 public static Cliente ModificarCliente(int numeroRUT, Cliente cliente)
 {
     using (var context = new ServicesContext())
     {
         var encontrarCliente = context.clientes.SingleOrDefault(x => x.RUT == numeroRUT);
         if (encontrarCliente != null)
         {
             encontrarCliente.nombreCliente = cliente.nombreCliente.Trim();
             encontrarCliente.RUT           = cliente.RUT;
             encontrarCliente.cargoCliente  = cliente.cargoCliente.Trim();
             context.SaveChanges();
             return(encontrarCliente);
         }
         else
         {
             return(null);
         }
     }
 }
        public static Producto CrearProducto(Producto producto)
        {
            using (var context = new ServicesContext())
            {
                var encontrarProducto = context.productos.
                                        Where(x => x.nombreProducto.ToUpper() == producto.nombreProducto.ToUpper()).SingleOrDefault();

                if (encontrarProducto != null)
                {
                    return(null);
                }
                else
                {
                    context.productos.Add(producto);
                    context.SaveChanges();
                    var ultimoProducto = context.productos.OrderByDescending(x => x.ID).FirstOrDefault();
                    return(ultimoProducto);
                }
            }
        }
Esempio n. 18
0
        public static JArray CrearFactura(Factura factura)
        {
            using (var context = new ServicesContext())
            {
                context.facturas.Add(factura);
                context.SaveChanges();

                var ultimaFactura = context.facturas.OrderByDescending(x => x.ID).
                                    Select(x => new { x.ID, x.fecha, x.clienteFK }).Take(1).FirstOrDefault();

                for (int i = 0; i < factura.detallesVenta.Count; i++)
                {
                    factura.detallesVenta.ElementAt(i).facturaFK = ultimaFactura.ID;
                }
                ServiciosDetallesFactura.CrearDetallesFactura(factura.detallesVenta);

                JArray arrayJSON = BuscarFactura(ultimaFactura.ID);
                return(arrayJSON);
            }
        }
Esempio n. 19
0
        public static Category DeleteCategory(int categoryID)
        {
            using (var context = new ServicesContext())
            {
                var selectedCategory = context.Categories.FirstOrDefault(b => b.ID == categoryID);

                // Checking if the category exists within the database.
                if (selectedCategory != null)
                {
                    context.Categories.Remove(selectedCategory);
                    context.SaveChanges();

                    return(selectedCategory);
                }
                // If it does not, return null.
                else
                {
                    return(null);
                }
            }
        }
        public static MonthlyStats DeleteMonthlyStat(int statID)
        {
            using (var context = new ServicesContext())
            {
                var selectedMonthlyStat = context.monthlyStats.FirstOrDefault(b => b.ID == statID);

                // Checking if the monthly stats exists within the database.
                if (selectedMonthlyStat != null)
                {
                    context.monthlyStats.Remove(selectedMonthlyStat);
                    context.SaveChanges();

                    return(selectedMonthlyStat);
                }
                // If it does not, return null.
                else
                {
                    return(null);
                }
            }
        }
        public static DailyStats CreateDailyStat(DailyStats dailyStat)
        {
            using (var context = new ServicesContext())
            {
                // Check if there's already a daily stat for this day.
                var findDailyStat = context.dailyStats.FirstOrDefault
                                        (b => b.salesDate == dailyStat.salesDate);

                // If there isn't, create one.
                if (findDailyStat == null)
                {
                    context.dailyStats.Add(dailyStat);
                    context.SaveChanges();
                    return(dailyStat);
                }
                // If there is, return null and let the controller handle it.
                else
                {
                    return(null);
                }
            }
        }
Esempio n. 22
0
        public static Category UpdateCategory(int categoryID, Category category)
        {
            using (var context = new ServicesContext())
            {
                var selectedCategory = context.Categories.FirstOrDefault(b => b.ID == categoryID);

                // Checking if the category does exist within the database.
                if (selectedCategory != null)
                {
                    selectedCategory.categoryName    = category.categoryName.Trim();
                    selectedCategory.ammountProducts = category.ammountProducts;
                    context.SaveChanges();

                    return(selectedCategory);
                }
                // If it does not, return null.
                else
                {
                    return(null);
                }
            }
        }
        public static DailyStats UpdateDailyStat(int statID, DailyStats dailyStat)
        {
            using (var context = new ServicesContext())
            {
                var selectedStat = context.dailyStats.FirstOrDefault(b => b.ID == statID);

                // Checking if the stat does exist within the database.
                if (selectedStat != null)
                {
                    selectedStat.salesDate    = dailyStat.salesDate;
                    selectedStat.productsSold = dailyStat.productsSold;
                    context.SaveChanges();

                    return(selectedStat);
                }
                // If it does not, return null.
                else
                {
                    return(null);
                }
            }
        }
        public static MonthlyStats CreateMonthlyStat(MonthlyStats monthlyStat)
        {
            using (var context = new ServicesContext())
            {
                // Check if there's already a stat for this month.
                var findMonthlyStat = context.monthlyStats.FirstOrDefault
                                          (b => b.salesMonth == monthlyStat.salesMonth && b.salesYear == monthlyStat.salesYear);

                // If there isn't, create one.
                if (findMonthlyStat == null)
                {
                    context.monthlyStats.Add(monthlyStat);
                    context.SaveChanges();
                    return(monthlyStat);
                }
                // If there is, return null and let the controller handle it.
                else
                {
                    return(null);
                }
            }
        }
        public static Cliente CrearCliente(Cliente cliente)
        {
            string nombreCliente = cliente.nombreCliente.ToUpper();

            using (var context = new ServicesContext())
            {
                var encontrarCliente = context.clientes.SingleOrDefault(
                    x => x.nombreCliente.ToUpper() == nombreCliente);

                if (encontrarCliente != null)
                {
                    return(null);
                }
                else
                {
                    context.clientes.Add(cliente);
                    context.SaveChanges();
                    var ultimoCliente = context.clientes.OrderByDescending(x => x.ID).FirstOrDefault();
                    return(ultimoCliente);
                }
            }
        }
        public static MonthlyStats UpdateMonthlyStat(int statID, MonthlyStats monthlyStat)
        {
            using (var context = new ServicesContext())
            {
                var selectedStat = context.monthlyStats.FirstOrDefault(b => b.ID == statID);

                // Checking if the stat does exist within the database.
                if (selectedStat != null)
                {
                    selectedStat.salesMonth = monthlyStat.salesMonth;
                    selectedStat.salesYear  = monthlyStat.salesYear;
                    context.SaveChanges();

                    return(selectedStat);
                }
                // If it does not, return null.
                else
                {
                    return(null);
                }
            }
        }
Esempio n. 27
0
        public static Sale UpdateSale(int saleID, Sale sale)
        {
            using (var context = new ServicesContext())
            {
                var selectedSale = context.Sales.FirstOrDefault(b => b.ID == saleID);

                // Checking if the sale does exist within the database.
                if (selectedSale != null)
                {
                    selectedSale.saleDate        = sale.saleDate;
                    selectedSale.saleDescription = sale.saleDescription.Trim();
                    selectedSale.saleTotal       = sale.saleTotal;
                    context.SaveChanges();

                    return(selectedSale);
                }
                // If it does not, return null.
                else
                {
                    return(null);
                }
            }
        }