示例#1
0
        public PurchasedMeal Add(PurchasedMeal newMeal)
        {
            // Maybe do each of these in a different thread, so the web service is nice and speedy
            // if people click quickly.

            using (SqlConnection connection = new SqlConnection(Settings.DatabaseConnectionString))
            {
                SqlCommand sqlCommand = new SqlCommand
                {
                    Connection  = connection,
                    CommandType = CommandType.Text,
                    CommandText =
                        "INSERT INTO StudentMeal(iStudentID, iMealTypeID, dDate, nAmount, iSchoolID) VALUES(@STUDENTID, @MEALTYPE, @CURDATE, @AMOUNT, @SCHOOLID); "
                };

                sqlCommand.Parameters.AddWithValue("STUDENTID", newMeal.StudentID);
                sqlCommand.Parameters.AddWithValue("MEALTYPE", newMeal.MealType);
                sqlCommand.Parameters.AddWithValue("CURDATE", DateTime.Now);
                sqlCommand.Parameters.AddWithValue("AMOUNT", newMeal.Amount);
                sqlCommand.Parameters.AddWithValue("SCHOOLID", newMeal.SchoolID);

                sqlCommand.Connection.Open();
                newMeal.MealID = sqlCommand.ExecuteNonQuery();
                sqlCommand.Connection.Close();
            }

            return(newMeal);
        }
示例#2
0
        public PurchasedMeal Get(int id)
        {
            PurchasedMeal returnedMeal = new PurchasedMeal();

            using (SqlConnection connection = new SqlConnection(Settings.DatabaseConnectionString))
            {
                SqlCommand sqlCommand = new SqlCommand
                {
                    Connection  = connection,
                    CommandType = CommandType.Text,
                    CommandText =
                        "SELECT iStudentMealID, iStudentID, iMealTypeID, dDate, nAmount, iSchoolID FROM StudentMeal WHERE iStudentMealID=@MEALID ORDER BY iStudentMealID DESC;"
                };
                sqlCommand.Parameters.AddWithValue("MEALID", id);
                sqlCommand.Connection.Open();
                SqlDataReader dataReader = sqlCommand.ExecuteReader();

                if (dataReader.HasRows)
                {
                    while (dataReader.Read())
                    {
                        returnedMeal = dataReaderToPurchasedMeal(dataReader);
                    }
                }

                sqlCommand.Connection.Close();
            }
            return(returnedMeal);
        }
        // POST api/<controller>
        public HttpResponseMessage Post([FromBody] PurchasedMeal value)
        {
            try
            {
                Dictionary <int, MealType> allMealTypes = MealTypeRepository.GetDictionary();

                // Find the selected meal type
                if (allMealTypes.ContainsKey(value.MealType))
                {
                    MealType selectedMealType = allMealTypes[value.MealType];

                    if (value.Amount <= selectedMealType.FullAmount)
                    {
                        if (value.Amount >= selectedMealType.FullAmount * -1)
                        {
                            value = Repository.Add(value);

                            // Apparently we should be responding to a POST request with HTTP status 201 instead of 200, which would be the default
                            HttpResponseMessage response = Request.CreateResponse <PurchasedMeal>(HttpStatusCode.Created, value);
                            return(response);
                        }
                        else
                        {
                            return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, new Exception("Amount cannot be less than full price * -1")));
                        }
                    }
                    else
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, new Exception("Amount cannot be more than full price")));
                    }
                }
                else
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, new Exception("Invalid MealID")));
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex));
            }
        }
示例#4
0
        private async void HandleMealPurchase(Student student, MealType mealtype)
        {
            // The only information we need is a student database ID and a mealtype id
            // If we don't have that, don't bother continuing.
            if ((student == null) || (mealtype == null))
            {
                return;
            }


            // Figure out what this meal is going to cost
            decimal mealCost = mealtype.FullAmount;

            if (PriceMode == PriceMode.ReducedPrice)
            {
                mealCost = mealtype.ReducedAmount;
            }
            if (PriceMode == PriceMode.Free)
            {
                mealCost = mealtype.FreeAmount;
            }

            // Try to create a valid meal purchase object
            PurchasedMeal newMeal = new PurchasedMeal()
            {
                Amount      = mealCost,
                MealType    = mealtype.ID,
                SchoolID    = Settings.SchoolDatabaseID,
                StudentID   = student.ID,
                DateAndTime = DateTime.Now,
                Student     = student,
                MealInfo    = mealtype
            };

            if (newMeal.IsValid())
            {
                if (voidMode)
                {
                    // Find the last entry for the entered student and reverse it
                    bool foundPreviousEntry = false;
                    foreach (PurchasedMeal pmeal in mealLog.Where(p => p.Voided == false))
                    {
                        if (pmeal.StudentID == student.ID)
                        {
                            foundPreviousEntry = true;
                            pmeal.Voided       = true;
                            newMeal.Amount     = pmeal.Amount * -1;
                            newMeal.Voided     = true;
                            break;
                        }
                    }

                    voidMode = false;
                    RefreshUI();

                    // If there is no previous entry to void, don't try to post anything, just ignore this one.
                    if (foundPreviousEntry == false)
                    {
                        return;
                    }
                }

                // Try to push to the web API
                try
                {
                    using (HttpClient client = new HttpClient())
                    {
                        client.BaseAddress = new Uri(Settings.ServerURL);
                        client.DefaultRequestHeaders.Accept.Clear();
                        client.DefaultRequestHeaders.Accept.Add(
                            new MediaTypeWithQualityHeaderValue("application/json"));

                        HttpResponseMessage response = await client.PostAsJsonAsync("api/PurchasedMeal", newMeal);

                        if (response.IsSuccessStatusCode)
                        {
                            mealLog.Add(newMeal);
                        }
                        else
                        {
                            MessageBox.Show(response.StatusCode.ToString());
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                MessageBox.Show("Invalid meal constructed - unable to post\n" + newMeal,
                                "Invalid PurchasedMeal object", MessageBoxButton.OK, MessageBoxImage.Error);
            }

            // Scroll to the last item scanned
            listMealLog.Items.MoveCurrentToLast();
            listMealLog.ScrollIntoView(listMealLog.Items.CurrentItem);
        }