public IHttpActionResult Post([FromBody] Sale sale)
        {
            string           path = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "RestfulTestfulFiles");
            SQLiteConnection db   = new SQLiteConnection(System.IO.Path.Combine(path, "RestfulTestfulDatabase.db"));

            sale.TokenNumber = 1;
            DateTime now = DateTime.Now;

            sale.AddDate = now;
            if (!POEChecker.AlreadyIsInDatabase(sale))
            {
                if (db.Table <Product>().Where(p => p.ID.Equals(sale.ProductID)).Any() && db.Table <Client>().Where(c => c.ID.Equals(sale.ClientID)).Any())
                {
                    Product product = db.Table <Product>().Where(p => p.ID.Equals(sale.ProductID)).First();
                    if (product.Quantity > sale.Quantity && !product.Discontinued)
                    {
                        product.Quantity = product.Quantity - sale.Quantity;
                        product.TokenNumber++;
                        if (db.Update(product) == 1 && db.Insert(sale) == 1)
                        {
                            return(Ok <SaleResponseModel>(new SaleResponseModel(db.GetAllWithChildren <Sale>().Last(s => s.ProductID.Equals(sale.ProductID) && s.ClientID.Equals(sale.ClientID) && s.AddDate.Equals(now)), this.Url, true)));
                        }
                        return(InternalServerError(new Exception("Couldn't insert row into database")));
                    }
                    return(BadRequest("Insufficient product quantity or product discontinued"));
                }
                return(BadRequest("Invalid product or/and client ID"));
            }
            return(BadRequest("Object already is in database!"));
        }
 public IHttpActionResult Post([FromBody] Product product)
 {
     if (product.Name != null && product.Category != null)
     {
         string           path = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "RestfulTestfulFiles");
         SQLiteConnection db   = new SQLiteConnection(System.IO.Path.Combine(path, "RestfulTestfulDatabase.db"));
         product.TokenNumber = 1;
         if (!POEChecker.AlreadyIsInDatabase(product))
         {
             if (db.Insert(product) == 1)
             {
                 return(Ok <ProductResponseModel>(new ProductResponseModel(db.Table <Product>().Last(p => p.Name.Equals(product.Name) && p.Category.Equals(product.Category) && p.Price.Equals(product.Price) && p.Discount.Equals(product.Discount)), this.Url)));
             }
             return(InternalServerError(new Exception("Couldn't insert row into database")));
         }
         return(BadRequest("Object already is in database!"));
     }
     return(BadRequest("Product name and category can't be null!."));
 }
Exemple #3
0
 public IHttpActionResult Post([FromBody] Client client)
 {
     if (client.Name != null && client.Surname != null && client.Address != null)
     {
         string           path = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "RestfulTestfulFiles");
         SQLiteConnection db   = new SQLiteConnection(System.IO.Path.Combine(path, "RestfulTestfulDatabase.db"));
         client.TokenNumber = 1;
         if (!POEChecker.AlreadyIsInDatabase(client))
         {
             if (db.Insert(client) == 1)
             {
                 ClientResponseModel clientResponseModel = new ClientResponseModel(db.Table <Client>().Last(c => c.Name.Equals(client.Name) && c.Surname.Equals(client.Surname) && c.PhoneNumber.Equals(client.PhoneNumber) && c.Address.Equals(client.Address)), this.Url);
                 return(Ok <ClientResponseModel>(clientResponseModel));
             }
             return(InternalServerError(new Exception("Couldn't insert row into database")));
         }
         return(BadRequest("Object already is in database!"));
     }
     return(BadRequest("Client name, surname and address can't be null"));
 }
Exemple #4
0
 public IHttpActionResult Post([FromBody] Employee employee)
 {
     if (employee.Name != null && employee.Name.Length > 0)
     {
         if (employee.Password != null && employee.Password.Length > 6 &&
             Regex.IsMatch(employee.Password, @"[!,@,#,$,%,^,&,*,?,_,~,-,£,(,)]") &&
             Regex.IsMatch(employee.Password, @"\d+") &&
             Regex.IsMatch(employee.Password, @"[a-z]") &&
             Regex.IsMatch(employee.Password, @"[A-Z]"))
         {
             string           path = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "RestfulTestfulFiles");
             SQLiteConnection db   = new SQLiteConnection(System.IO.Path.Combine(path, "RestfulTestfulDatabase.db"));
             employee.TokenNumber = 1;
             if (!POEChecker.AlreadyIsInDatabase(employee))
             {
                 if (db.Insert(employee) == 1)
                 {
                     long nextEmployeeID     = 0;
                     long previousEmployeeID = 0;
                     int  employeeIndex      = db.Table <Employee>().ToList().IndexOf(employee);
                     if (employeeIndex > 0)
                     {
                         previousEmployeeID = db.Table <Employee>().ToList()[employeeIndex - 1].ID;
                     }
                     if (employeeIndex < db.Table <Employee>().Count() - 1)
                     {
                         nextEmployeeID = db.Table <Employee>().ToList()[employeeIndex + 1].ID;
                     }
                     return(Ok <EmployeeResponseModel>(new EmployeeResponseModel(db.Table <Employee>().Last(e => e.Name.Equals(employee.Name) && e.Password.Equals(employee.Password)), this.Url, nextEmployeeID, previousEmployeeID)));
                 }
                 return(InternalServerError());
             }
             return(BadRequest("Object already is in database!"));
         }
         return(BadRequest("Your password must be at least 6 characters long and must contain at least one special, at least one number, at least one small letter and at least one capital letter"));
     }
     return(BadRequest("Your nickname is too short"));
 }