public IHttpActionResult Update(WeightTicket weightTicket)
        {
            WeightTicketResponse response = new WeightTicketResponse();

            try
            {
                weightTicket.UserId = CurrentUserId;
                WeightTicket weightTicketSaved = weightTicketsBL.UpdateWeightTicket(weightTicket);
                response.WeightTicket = weightTicketSaved;
                response.Success      = true;
            }
            catch (WeightTicketException ex)
            {
                response.ErrorCode    = ex.Error;
                response.ErrorMessage = "Error. " + ex.Error.ToString();
                response.WeightTicket = null;
                response.Success      = false;
            }
            catch (Exception ex)
            {
                response.ErrorMessage = "Error. " + ex.Message;
                response.WeightTicket = null;
                response.Success      = false;
            }
            return(Ok(response));
        }
Exemple #2
0
 public void SaveWeightTicket()
 {
     try
     {
         bool       reLoadList = false;
         HttpClient client     = new HttpClient();
         client.BaseAddress = new Uri(baseUrl);
         client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/bson"));
         client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Token.access_token);
         string action = string.Empty;
         if (view.CurrentWeightTicket.Id == 0)
         {
             //insert
             action     = insertAction;
             reLoadList = true;
         }
         else
         {
             action = updateAction;
         }
         //update
         MediaTypeFormatter  bsonFormatter = new BsonMediaTypeFormatter();
         HttpResponseMessage response      = client.PostAsync(action, view.CurrentWeightTicket, bsonFormatter).Result;
         response.EnsureSuccessStatusCode();
         MediaTypeFormatter[] formatters           = new MediaTypeFormatter[] { bsonFormatter };
         WeightTicketResponse weightTicketResponse = response.Content.ReadAsAsync <WeightTicketResponse>(formatters).Result;
         if (weightTicketResponse.Success)
         {
             if (weightTicketResponse.WeightTicket != null)
             {
                 if (reLoadList)
                 {
                     LoadWeightTickets();
                     view.SelectedId = weightTicketResponse.WeightTicket.Id;
                 }
             }
         }
         else
         {
             throw new WeightTicketException(weightTicketResponse.ErrorCode, weightTicketResponse.ErrorMessage);
         }
     }
     catch (Exception ex)
     {
         StackTrace st = new StackTrace();
         StackFrame sf = st.GetFrame(0);
         MethodBase currentMethodName = sf.GetMethod();
         Guid       errorId           = Guid.NewGuid();
         //Log error here
         view.HandleException(ex, currentMethodName.Name, errorId);
     }
 }
Exemple #3
0
        public void TestDeleteErrors()
        {
            Token token = TokenHelper.GetToken(baseUrl, "Melvin3", "MelvinPass3");
            JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
            HttpClient           client = new HttpClient();

            client.BaseAddress = new Uri(baseUrl);
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.access_token);
            //Test delete
            HttpResponseMessage response = client.PostAsJsonAsync(deleteAction, new IdModel(0)).Result;

            Assert.IsTrue(response.IsSuccessStatusCode);
            WeightTicketResponse weightTicketResponse = response.Content.ReadAsAsync <WeightTicketResponse>().Result;

            Assert.IsFalse(weightTicketResponse.Success);
            Assert.IsTrue(weightTicketResponse.ErrorCode.HasFlag(WeightTicketError.InvalidId));
        }
        public IHttpActionResult Delete(IdModel id)
        {
            WeightTicketResponse response = new WeightTicketResponse();

            try
            {
                bool success = weightTicketsBL.DeleteWeightTicket(id.Id);
                response.Success = success;
            }
            catch (WeightTicketException ex)
            {
                response.ErrorCode    = ex.Error;
                response.ErrorMessage = "Error. " + ex.Error.ToString();
                response.WeightTicket = null;
                response.Success      = false;
            }
            catch (Exception ex)
            {
                response.ErrorMessage = "Error. " + ex.Message;
                response.WeightTicket = null;
                response.Success      = false;
            }
            return(Ok(response));
        }
Exemple #5
0
        public void TestInsertUpdateErrors()
        {
            Token token = TokenHelper.GetToken(baseUrl, "Melvin3", "MelvinPass3");
            JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
            //Test insert
            WeightTicket weightTicket = new WeightTicket();
            string       jsonTicket   = javaScriptSerializer.Serialize(weightTicket);
            //Post add ticket
            HttpClient client = new HttpClient();

            client.BaseAddress = new Uri(baseUrl);
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.access_token);
            //Test insert
            HttpResponseMessage response = client.PostAsJsonAsync(insertAction, weightTicket).Result;

            Assert.IsTrue(response.IsSuccessStatusCode);
            WeightTicketResponse weightTicketResponse = response.Content.ReadAsAsync <WeightTicketResponse>().Result;

            Assert.IsFalse(weightTicketResponse.Success);
            Assert.IsTrue(weightTicketResponse.ErrorCode.HasFlag(WeightTicketError.InvalidCicle));
            Assert.IsTrue(weightTicketResponse.ErrorCode.HasFlag(WeightTicketError.InvalidFolio));
            Assert.IsTrue(weightTicketResponse.ErrorCode.HasFlag(WeightTicketError.InvalidProducer));
            Assert.IsTrue(weightTicketResponse.ErrorCode.HasFlag(WeightTicketError.InvalidWareHouse));
            Assert.IsTrue(weightTicketResponse.ErrorCode.HasFlag(WeightTicketError.InvalidProduct));
            //Test update
            response = client.PostAsJsonAsync(updateAction, weightTicket).Result;
            Assert.IsTrue(response.IsSuccessStatusCode);
            weightTicketResponse = response.Content.ReadAsAsync <WeightTicketResponse>().Result;
            Assert.IsFalse(weightTicketResponse.Success);
            Assert.IsTrue(weightTicketResponse.ErrorCode.HasFlag(WeightTicketError.InvalidCicle));
            Assert.IsTrue(weightTicketResponse.ErrorCode.HasFlag(WeightTicketError.InvalidFolio));
            Assert.IsTrue(weightTicketResponse.ErrorCode.HasFlag(WeightTicketError.InvalidProducer));
            Assert.IsTrue(weightTicketResponse.ErrorCode.HasFlag(WeightTicketError.InvalidWareHouse));
            Assert.IsTrue(weightTicketResponse.ErrorCode.HasFlag(WeightTicketError.InvalidProduct));
        }
Exemple #6
0
        public void TestInsertUpdateAndGetWeightTicket()
        {
            //Get token
            Token token = TokenHelper.GetToken(baseUrl, "Melvin3", "MelvinPass3");
            JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
            //Test insert
            WeightTicket weightTicket = WeightTicketHelper.CreateDummyTicket();
            string       jsonTicket   = javaScriptSerializer.Serialize(weightTicket);
            //Post add ticket
            HttpClient client = new HttpClient();

            client.BaseAddress = new Uri(baseUrl);
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.access_token);
            HttpResponseMessage response = client.PostAsJsonAsync(insertAction, weightTicket).Result;

            Assert.IsTrue(response.IsSuccessStatusCode);
            WeightTicketResponse weightTicketResponse = response.Content.ReadAsAsync <WeightTicketResponse>().Result;

            Assert.IsTrue(weightTicketResponse.Success);
            Assert.IsTrue(weightTicketResponse != null);
            Assert.IsTrue(weightTicketResponse.WeightTicket.Id > 0);
            //get by id
            string getByIdUrl = string.Format("{0}?id={1}", getByIdAction, weightTicketResponse.WeightTicket.Id.ToString());

            response = client.GetAsync(getByIdUrl).Result;
            Assert.IsTrue(response.IsSuccessStatusCode);
            GetWeightTicketResponse getWeightTicketResponse = response.Content.ReadAsAsync <GetWeightTicketResponse>().Result;

            Assert.IsTrue(getWeightTicketResponse.Success);
            Assert.IsTrue(getWeightTicketResponse.WeightTickets.Count == 1);
            Assert.IsTrue(getWeightTicketResponse.WeightTickets.ElementAt(0).Id == weightTicketResponse.WeightTicket.Id);
            //get all
            response = client.GetAsync(getAllAction).Result;
            Assert.IsTrue(response.IsSuccessStatusCode);
            getWeightTicketResponse = response.Content.ReadAsAsync <GetWeightTicketResponse>().Result;
            Assert.IsTrue(getWeightTicketResponse.Success);
            WeightTicket ticketFound = (from ticket in getWeightTicketResponse.WeightTickets where ticket.Id == weightTicketResponse.WeightTicket.Id select ticket).FirstOrDefault();

            Assert.IsTrue(ticketFound != null);
            //test update
            weightTicket.Id          = weightTicketResponse.WeightTicket.Id;
            weightTicket.SubTotal    = 20;
            weightTicket.ApplyDrying = false; //Todo check other properties
            response = client.PostAsJsonAsync(updateAction, weightTicket).Result;
            Assert.IsTrue(response.IsSuccessStatusCode);
            weightTicketResponse = response.Content.ReadAsAsync <WeightTicketResponse>().Result;
            Assert.IsTrue(weightTicketResponse.Success);
            //Get ticket again and check it was updated
            response = client.GetAsync(getByIdUrl).Result;
            Assert.IsTrue(response.IsSuccessStatusCode);
            getWeightTicketResponse = response.Content.ReadAsAsync <GetWeightTicketResponse>().Result;
            Assert.IsTrue(getWeightTicketResponse.Success);
            Assert.IsTrue(getWeightTicketResponse.WeightTickets.Count == 1);
            Assert.IsTrue(getWeightTicketResponse.WeightTickets.ElementAt(0).SubTotal == 20);
            Assert.IsFalse(getWeightTicketResponse.WeightTickets.ElementAt(0).ApplyDrying);
            //test delete
            response = client.PostAsJsonAsync(deleteAction, new IdModel(weightTicketResponse.WeightTicket.Id)).Result;
            Assert.IsTrue(response.IsSuccessStatusCode);
            weightTicketResponse = response.Content.ReadAsAsync <WeightTicketResponse>().Result;
            Assert.IsTrue(weightTicketResponse.Success);
            //Get ticket again and check it is not found
            response = client.GetAsync(getByIdUrl).Result;
            Assert.IsTrue(response.IsSuccessStatusCode);
            getWeightTicketResponse = response.Content.ReadAsAsync <GetWeightTicketResponse>().Result;
            Assert.IsTrue(getWeightTicketResponse.Success);
            Assert.IsTrue(getWeightTicketResponse.WeightTickets.Count == 0);
        }