Exemple #1
0
 private bool ValidateDeleteRequest(CompanyPartsApiDeleteRequest req)
 {
     if (req.LoginToken == null || req.LoginToken.Equals("x''"))
     {
         return(false);
     }
     if (req.AuthToken == null || req.AuthToken.Equals("x''"))
     {
         return(false);
     }
     if (req.UserId <= 0)
     {
         return(false);
     }
     if (req.PartEntryId <= 0)
     {
         return(false);
     }
     return(true);
 }
Exemple #2
0
        /// <summary>
        /// PATCH request format located in the Web Api Enumeration v2
        /// under the tab Company/Parts, starting row 28
        /// </summary>
        /// <param name="ctx">HttpListenerContext to respond to</param>
        private void HandlePatchRequest(HttpListenerContext ctx)
        {
            try
            {
                #region Inpute Validation
                if (!ctx.Request.HasEntityBody)
                {
                    WriteBodyResponse(ctx, 400, "Bad Request", "No Body");
                    return;
                }

                string reqStr;
                using (var reader = new StreamReader(ctx.Request.InputStream))
                {
                    reqStr = reader.ReadToEnd();
                }
                CompanyPartsApiPatchRequest entry = JsonDataObjectUtil <CompanyPartsApiPatchRequest> .ParseObject(reqStr);

                if (!ValidatePatchRequest(entry))
                {
                    CompanyPartsApiDeleteRequest entry2 = JsonDataObjectUtil <CompanyPartsApiDeleteRequest> .ParseObject(reqStr);

                    if (entry2 != null)
                    {
                        HandleDeleteRequest(ctx, entry2);
                        return;
                    }
                    WriteBodyResponse(ctx, 400, "Bad Request", "Incorrect Format");
                    return;
                }
                #endregion

                MySqlDataManipulator connection = new MySqlDataManipulator();
                using (connection)
                {
                    bool res = connection.Connect(MySqlDataManipulator.GlobalConfiguration.GetConnectionString());
                    if (!res)
                    {
                        WriteBodyResponse(ctx, 500, "Unexpected Server Error", "Connection to database failed");
                        return;
                    }
                    #region User Validation
                    OverallUser mappedUser = connection.GetUserById(entry.UserId);
                    if (mappedUser == null)
                    {
                        WriteBodyResponse(ctx, 404, "Not Found", "User was not found on on the server");
                        return;
                    }
                    if (!UserVerificationUtil.LoginTokenValid(mappedUser, entry.LoginToken))
                    {
                        WriteBodyResponse(ctx, 401, "Not Authorized", "Login token was incorrect.");
                        return;
                    }
                    if (!UserVerificationUtil.AuthTokenValid(mappedUser, entry.AuthToken))
                    {
                        WriteBodyResponse(ctx, 401, "Not Authorized", "Auth token was ezpired or incorrect");
                        return;
                    }
                    if ((mappedUser.AccessLevel & AccessLevelMasks.PartMask) == 0)
                    {
                        WriteBodyResponse(ctx, 401, "Not Autherized", "Not marked as a Parts User");
                        return;
                    }
                    #endregion

                    #region Edit Parts Entry
                    var partEntry = connection.GetPartCatalogueEntryById(mappedUser.Company, entry.PartEntryId);

                    if (partEntry == null)
                    {
                        WriteBodyResponse(ctx, 404, "Not Found", "Part entry with the given id was not found");
                        return;
                    }

                    switch (entry.FieldName)
                    {
                    case "Make":
                        partEntry.Make = entry.FieldValue;
                        break;

                    case "Model":
                        partEntry.Model = entry.FieldValue;
                        break;

                    case "Year":
                        partEntry.Year = int.Parse(entry.FieldValue);
                        break;

                    case "PartId":
                        partEntry.PartId = entry.FieldValue;
                        break;

                    case "PartName":
                        partEntry.PartName = entry.FieldValue;
                        break;

                    default:
                        WriteBodyResponse(ctx, 404, "Not Found", "The field specified was not found");
                        return;
                    }

                    if (!connection.UpdatePartEntry(mappedUser.Company, partEntry))
                    {
                        WriteBodyResponse(ctx, 500, "Internal Server Error", "Error occured while removing part entry: " + connection.LastException.Message);
                        return;
                    }
                    WriteBodylessResponse(ctx, 200, "OK");
                    #endregion
                }
            }
            catch (HttpListenerException)
            {
                //HttpListeners dispose themselves when an exception occurs, so we can do no more.
            }
            catch (Exception e)
            {
                WriteBodyResponse(ctx, 500, "Internal Server Error", e.Message);
            }
        }
Exemple #3
0
        /// <summary>
        /// DELETE request format located in the Web Api Enumeration v2
        /// under the tab Company/Parts, starting row 51
        /// </summary>
        /// <param name="ctx">HttpListenerContext to respond to</param>
        private void HandleDeleteRequest(HttpListenerContext ctx, CompanyPartsApiDeleteRequest req)
        {
            try
            {
                #region Input Validation
                if (!ValidateDeleteRequest(req))
                {
                    WriteBodyResponse(ctx, 400, "Bad Request", "Incorrect Format");
                    return;
                }
                #endregion

                MySqlDataManipulator connection = new MySqlDataManipulator();
                using (connection)
                {
                    bool res = connection.Connect(MySqlDataManipulator.GlobalConfiguration.GetConnectionString());
                    if (!res)
                    {
                        WriteBodyResponse(ctx, 500, "Unexpected Server Error", "Connection to database failed");
                        return;
                    }
                    #region User Validation
                    OverallUser mappedUser = connection.GetUserById(req.UserId);
                    if (mappedUser == null)
                    {
                        WriteBodyResponse(ctx, 404, "Not Found", "User was not found on on the server");
                        return;
                    }
                    if (!UserVerificationUtil.LoginTokenValid(mappedUser, req.LoginToken))
                    {
                        WriteBodyResponse(ctx, 401, "Not Authorized", "Login token was incorrect.");
                        return;
                    }
                    if (!UserVerificationUtil.AuthTokenValid(mappedUser, req.AuthToken))
                    {
                        WriteBodyResponse(ctx, 401, "Not Authorized", "Auth token was ezpired or incorrect");
                        return;
                    }
                    if ((mappedUser.AccessLevel & AccessLevelMasks.PartMask) == 0)
                    {
                        WriteBodyResponse(ctx, 401, "Not Autherized", "Not marked as a Parts User");
                        return;
                    }
                    #endregion

                    #region Delete Parts Request
                    if (connection.GetPartCatalogueEntryById(mappedUser.Company, req.PartEntryId) == null)
                    {
                        WriteBodyResponse(ctx, 404, "Not Found", "Part entry with the given id was not found");
                        return;
                    }

                    if (!connection.RemovePartCatalogueEntry(mappedUser.Company, req.PartEntryId))
                    {
                        WriteBodyResponse(ctx, 500, "Internal Server Error", "Error occured while removing part entry: " + connection.LastException.Message);
                        return;
                    }
                    WriteBodylessResponse(ctx, 200, "OK");
                    #endregion
                }
            }
            catch (HttpListenerException)
            {
                //HttpListeners dispose themselves when an exception occurs, so we can do no more.
            }
            catch (Exception e)
            {
                WriteBodyResponse(ctx, 500, "Internal Server Error", e.Message);
            }
        }