/// <summary> /// Remove a Item from Sale /// </summary> /// <returns></returns> public Response RemoveItem(Objects.SaleItem item) { int statusCode = 204; Response response = new Response(); try { Database.Connection.Open(); string Query = "DELETE FROM SaleItems WHERE SaleItemId=@SaleItemId "; MySqlCommand Command = new MySqlCommand(Query, Database.Connection); Command.Parameters.AddWithValue("SaleItemId", item.Id); Command.ExecuteNonQuery(); Database.Connection.Close(); // OK : Successfully Deleted -> Returning No Content statusCode = 204; response.StatusCode = statusCode; } catch (MySqlException e) { Logger.QueryError(e, "Sale", "Remove Item"); // Internal Error : response.StatusCode = 500; } if (Database.Connection.State == ConnectionState.Open) { Database.Connection.Close(); } return(response); }
/// <summary> /// Get Item of Sale /// </summary> /// <returns></returns> public Response GetSaleItems(Objects.Sale sale) { int statusCode = 200; Response response = new Response(); List <object> items = new List <object>(); try { Database.Connection.Open(); string query = " SELECT * FROM SaleItems WHERE SaleId=@SaleId ORDER BY CreatedAt DESC "; //Execute Query MySqlCommand Command = new MySqlCommand(query, Database.Connection); Command.Parameters.AddWithValue("SaleId", sale.Id); MySqlDataReader DataReader = Command.ExecuteReader(); //Read Data while (DataReader.Read()) { Objects.SaleItem item = new Objects.SaleItem { Id = DataReader["SaleItemId"].ToString(), SaleId = DataReader["SaleId"].ToString(), Note = DataReader["Note"].ToString(), Status = DataReader["Status"].ToString(), Quantity = decimal.Parse(DataReader["Quantity"].ToString()), DiscountPrice = decimal.Parse(DataReader["DiscountPrice"].ToString()), Price = decimal.Parse(DataReader["Price"].ToString()), TaxAmount = decimal.Parse(DataReader["TaxAmount"].ToString()), TotalPrice = decimal.Parse(DataReader["TotalPrice"].ToString()), IsDiscounted = Func.ToBoolean(DataReader["IsDiscounted"].ToString()), Product = new Objects.Product { Id = DataReader["ProductId"].ToString() }, Discount = new Objects.Discount { Id = DataReader["DiscountId"].ToString() }, SoldAt = Convert.ToDateTime(DataReader["CreatedAt"].ToString()), IsFetchItem = true, }; items.Add(item); } if (!DataReader.HasRows) { // Nothing statusCode = 204; } else { // OK statusCode = 200; } Database.Connection.Close(); //Response response.StatusCode = statusCode; response.DataArray = items; } catch (MySqlException e) { Logger.QueryError(e, "Sale", "Getting Sale Items"); // Internal Error : response.StatusCode = 500; } //Close Connection if Open if (Database.Connection.State == ConnectionState.Open) { Database.Connection.Close(); } return(response); }
/// <summary> /// Add a Item to a Sale /// </summary> /// <returns></returns> public Response AddItem(Objects.SaleItem item, Objects.Sale sale) { int statusCode = 201; Response response = new Response(); item.Id = ServerUtil.GenerateToken(TokenLengths.TokensLength); try { Database.Connection.Open(); string query = " INSERT INTO SaleItems (SaleItemId, SaleId, ProductId, DiscountId, Quantity, SupplyPrice, Price, DiscountPrice, TaxAmount, " + " TotalPrice, Note, Status, IsDiscounted, IsBalanceRequired )" + " VALUES (@SaleItemId,@SaleId, @ProductId, @DiscountId, @Quantity, @SupplyPrice, @Price, @DiscountPrice, @TaxAmount, " + " @TotalPrice, @Note, @Status, @IsDiscounted, @IsBalanceRequired ) "; MySqlCommand Command = new MySqlCommand(query, Database.Connection); Command.Parameters.AddWithValue("SaleItemId", item.Id); Command.Parameters.AddWithValue("SaleId", sale.Id); Command.Parameters.AddWithValue("ProductId", item.Product.Id); Command.Parameters.AddWithValue("DiscountId", item.Discount.Id); Command.Parameters.AddWithValue("Quantity", item.Quantity); Command.Parameters.AddWithValue("SupplyPrice", item.SupplyPrice); Command.Parameters.AddWithValue("Price", item.Price); Command.Parameters.AddWithValue("DiscountPrice", item.DiscountPrice); Command.Parameters.AddWithValue("TaxAmount", item.TaxAmount); Command.Parameters.AddWithValue("TotalPrice", item.TotalPrice); Command.Parameters.AddWithValue("Note", item.Note); Command.Parameters.AddWithValue("Status", item.Status); Command.Parameters.AddWithValue("IsDiscounted", Func.ToString(item.IsDiscounted)); Command.Parameters.AddWithValue("IsBalanceRequired", Func.ToString(item.IsBalanceRequired)); Command.ExecuteNonQuery(); Database.Connection.Close(); // Created statusCode = 201; } catch (MySqlException e) { if (e.Message.Contains("Duplicate entry")) { //Duplicate Key or Conflict statusCode = 409; } else { // Internal Server Error statusCode = 500; } Logger.QueryError(e, "Sale", "AddProduct"); } //Close Connection if Open if (Database.Connection.State == ConnectionState.Open) { Database.Connection.Close(); } //Response response.StatusCode = statusCode; response.Data = item; return(response); }