Exemple #1
0
        public ApiResponse AddPlant(AddPlantRequest request)
        {
            ApiResponse response = new ApiResponse();

            if (inventoryManager.InventoryNameIsNotUnique(request.Inventory.InventoryName))
            {
                response.AddMessage("InventoryName", new List <string>()
                {
                    "This inventory name is in use. Please choose another."
                });
            }
            else if (inventoryManager.PlantNameIsNotUnique(request.Plant.PlantName))
            {
                response.AddMessage("PlantName", new List <string>()
                {
                    "This plant name is in use. Please choose another."
                });
            }
            else
            {
                response.Id = inventoryManager.AddPlant(request);

                if (response.Id == 0)
                {
                    response.AddMessage("DbError", new List <string>()
                    {
                        "There was an error saving this plant."
                    });
                }
            }

            return(response);
        }
Exemple #2
0
 public long AddPlant(AddPlantRequest plantRequest)
 {
     return(persistence.AddPlant(plantRequest));
 }
Exemple #3
0
        public void AddPlant()
        {
            long image_id = 0;

            try
            {
                List <string> errorMsgs = new List <string>();

                KeyValuePair <long, string> p = (KeyValuePair <long, string>) this.PlantNames.SelectedValue;

                long   plantNameId            = p.Key;
                String plantName              = p.Value;
                long   selectedPlantTypeIndex = ((KeyValuePair <long, string>) this.PlantTypes.SelectedValue).Key;
                //long selectedServiceCode = ((KeyValuePair<long, string>)this.ServiceCodes.SelectedValue).Key;

                if (String.IsNullOrEmpty(plantName))
                {
                    errorMsgs.Add("You must enter a plant name.");
                }

                if (selectedPlantTypeIndex == 0)
                {
                    errorMsgs.Add("You must select a plant type.");
                }

                //if(selectedServiceCode == 0)
                //{
                //    errorMsgs.Add("You must select a service code.");
                //}

                if (fileStreamContent == null)
                {
                    MessageBoxResult msgBoxResult = MessageBox.Show("Do you want to add an image?");

                    if (msgBoxResult == MessageBoxResult.Yes)
                    {
                        return;
                    }
                }

                if (errorMsgs.Count > 0)
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (string msg in errorMsgs)
                    {
                        sb.AppendLine(msg);
                    }

                    MessageBox.Show(sb.ToString());
                    return;
                }

                AddPlantRequest addPlantRequest = new AddPlantRequest();

                //save image if the user has selected one
                if (fileStreamContent != null)
                {
                    var url = "http://localhost:9000/api/Login/UploadPlantImage";
                    fileStreamContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data")
                    {
                        Name = "file", FileName = fileName
                    };
                    fileStreamContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
                    using (var client1 = new HttpClient())
                    {
                        using (var formData = new MultipartFormDataContent())
                        {
                            client1.DefaultRequestHeaders.Add("EO-Header", wnd.User + " : " + wnd.Pwd);

                            formData.Add(fileStreamContent);
                            var    response   = client1.PostAsync(url, formData).Result;
                            string strImageId = response.Content.ReadAsStringAsync().Result;

                            Int64.TryParse(strImageId, out image_id);
                        }
                    }
                }

                PlantDTO pDTO = new PlantDTO();
                pDTO.PlantName        = plantName;
                pDTO.PlantTypeId      = selectedPlantTypeIndex;
                addPlantRequest.Plant = pDTO;

                InventoryDTO iDTO = new InventoryDTO();
                iDTO.InventoryName   = plantName;
                iDTO.InventoryTypeId = 1; //"Plants"
                //iDTO.ServiceCodeId = selectedServiceCode;

                addPlantRequest.Plant     = pDTO;
                addPlantRequest.Inventory = iDTO;
                if (image_id > 0)
                {
                    addPlantRequest.ImageId = image_id;
                }

                HttpClient client = new HttpClient();
                client.BaseAddress = new Uri(((App)App.Current).LAN_Address);
                client.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/json"));

                client.DefaultRequestHeaders.Add("EO-Header", wnd.User + " : " + wnd.Pwd);

                string jsonData = JsonConvert.SerializeObject(addPlantRequest);
                var    content  = new StringContent(jsonData, Encoding.UTF8, "application/json");
                HttpResponseMessage httpResponse = client.PostAsync("api/Login/AddPlant", content).Result;
                if (httpResponse.IsSuccessStatusCode)
                {
                    Stream       streamData = httpResponse.Content.ReadAsStreamAsync().Result;
                    StreamReader strReader  = new StreamReader(streamData);
                    string       strData    = strReader.ReadToEnd();
                    strReader.Close();
                    GetPlantResponse apiResponse = JsonConvert.DeserializeObject <GetPlantResponse>(strData);

                    if (apiResponse.Messages.Count > 0)
                    {
                        StringBuilder sb = new StringBuilder();
                        foreach (KeyValuePair <string, List <string> > messages in apiResponse.Messages)
                        {
                            foreach (string msg in messages.Value)
                            {
                                sb.AppendLine(msg);
                            }
                        }

                        MessageBox.Show(sb.ToString());
                    }
                    else
                    {
                        list3.Add(apiResponse.PlantInventoryList.First());
                        this.PlantInventoryListView.ItemsSource = null;
                        this.PlantInventoryListView.ItemsSource = list3;
                    }
                }
                else
                {
                    MessageBox.Show("Error adding plant");
                }

                fileStreamContent = null;
            }
            catch (Exception ex)
            {
            }
        }