Beispiel #1
0
        private JsonDictionaryStringConstructor WritePartsRequestToOutput(PartsRequest request, MySqlDataManipulator manipulator, int companyId)
        {
            JsonDictionaryStringConstructor ret = new JsonDictionaryStringConstructor();
            var user = manipulator.GetUserById(request.UserId);

            if (user == null)
            {
                ret.SetMapping("DisplayName", "defaultUser");
            }
            else
            {
                List <UserSettingsEntry> entries = JsonDataObjectUtil <List <UserSettingsEntry> > .ParseObject(user.Settings);

                ret.SetMapping("DisplayName", entries.Where(entry => entry.Key.Equals(UserSettingsEntryKeys.DisplayName)).First().Value);
            }
            List <int> referencedParts = JsonDataObjectUtil <List <int> > .ParseObject(request.ReferencedParts);

            JsonListStringConstructor partsConstructor = new JsonListStringConstructor();

            foreach (int i in referencedParts)
            {
                var part = manipulator.GetPartCatalogueEntryById(companyId, i);
                if (part == null)
                {
                    continue;
                }
                partsConstructor.AddElement(part.PartId);
            }
            ret.SetMapping("ReferencedParts", partsConstructor);
            ret.SetMapping("JobId", request.JobId);
            ret.SetMapping("Id", request.Id);
            return(ret);
        }
        public void TestDeserialize()
        {
            Assert.AreEqual(1, PartsRequest.Manipulator.InsertDataInto(TestConnection, TableName, Id1));
            PartsRequest toTest = PartsRequest.Manipulator.RetrieveDataWhere(TestConnection, TableName, "ReferencedParts=\"" + Id1.ReferencedParts + "\"")[0];

            Assert.AreEqual(Id1, toTest);
            Assert.AreEqual(1, PartsRequest.Manipulator.RemoveDataWhere(TestConnection, TableName, "ReferencedParts=\"" + Id1.ReferencedParts + "\""));
        }
Beispiel #3
0
        /// <summary>
        /// POST request format located in the Web Api Enumeration v2
        /// under the tab Company/Parts/Request, starting row 1
        /// </summary>
        /// <param name="ctx">HttpListenerContext to respond to</param>
        private void HandlePostRequest(HttpListenerContext ctx)
        {
            try
            {
                #region Input Validation
                if (!ctx.Request.HasEntityBody)
                {
                    WriteBodyResponse(ctx, 400, "Bad Request", "No Body");
                    return;
                }
                CompanyPartsRequestApiFullPostRequest entry = JsonDataObjectUtil <CompanyPartsRequestApiFullPostRequest> .ParseObject(ctx);

                if (!ValidateFullPostRequest(entry))
                {
                    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;
                    }
                    #endregion

                    #region Action Handling
                    //build request to send
                    PartsRequest request = new PartsRequest(entry.UserId, entry.RepairJobId, entry.PartsList);
                    //send request
                    res = connection.AddPartsRequest(entry.CompanyId, request);
                    if (!res)
                    {
                        WriteBodyResponse(ctx, 500, "Unexpected Server Error", 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);
            }
        }
Beispiel #4
0
        /// <summary>
        /// PUT request format located in the Web Api Enumeration v2
        /// under the tab Company/Parts/Request, starting row 49
        /// </summary>
        /// <param name="ctx">HttpListenerContext to respond to</param>
        private void HandlePutRequest(HttpListenerContext ctx)
        {
            try
            {
                #region Input 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();
                }
                CompanyPartsRequestPutRequest entry = JsonDataObjectUtil <CompanyPartsRequestPutRequest> .ParseObject(reqStr);

                if (!ValidatePutRequest(entry))
                {
                    CompanyPartsRequestGetRequest entry2 = JsonDataObjectUtil <CompanyPartsRequestGetRequest> .ParseObject(reqStr);

                    if (entry2 != null && ValidateGetRequest(entry2))
                    {
                        HandleGetRequest(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 Authorized", "User was not a parts level user");
                        return;
                    }
                    #endregion

                    #region Action Handling
                    PartsRequest request = connection.GetPartsRequestById(mappedUser.Company, entry.RequestId);
                    if (request == null)
                    {
                        WriteBodyResponse(ctx, 404, "Not Found", "Parts Request with the given id was not found on the server");
                        return;
                    }
                    if (!connection.RemovePartsRequest(mappedUser.Company, entry.RequestId, accept: true))
                    {
                        WriteBodyResponse(ctx, 500, "Internal Server Error", "Error ocurred while removing the parts request: " + 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);
            }
        }
 public void Init()
 {
     Id1 = new PartsRequest(1, "Whack LLC", "a, b, a");
     Id2 = new PartsRequest(1, "Mole Inc", "a, b, a");
     Id3 = new PartsRequest(1, "Whack LLC", "a, b, a");
 }