Exemplo n.º 1
0
        public IHttpActionResult AddMaintenance(OtherCostsModel maintenance)
        {
            try
            {
                var newMaintenance = new Maintenance()
                {
                    MaintenanceType = maintenance.MaintenanceType,
                    Vehicle         = DBContext.Vehicle.FirstOrDefault(x => x.RegNr == maintenance.Vehicle_Id),
                    ReportDate      = maintenance.ReportDate,
                    MaintenanceCost = maintenance.Cost
                };

                if (maintenance.Id != null)
                {
                    newMaintenance.Id = maintenance.Id.Value;
                }


                DBContext.Maintenance.AddOrUpdate(x => x.Id, newMaintenance);
                DBContext.SaveChanges();

                return(Ok());
            }
            catch (Exception)
            {
                return(BadRequest());
            }
        }
Exemplo n.º 2
0
        public async void AddMaintenance()
        {
            var costToSave   = new OtherCostsModel();
            var dialogResult = new MessageBoxResult();

            if (SelectedCost != null)
            {
                costToSave                 = SelectedCost;
                costToSave.Cost            = CostTextBox;
                costToSave.MaintenanceType = MaintenanceTypTextBox;
            }
            else
            {
                costToSave.Cost            = CostTextBox;
                costToSave.MaintenanceType = MaintenanceTypTextBox;
                costToSave.ReportDate      = DateTime.Now;
                costToSave.Vehicle_Id      = Vehicle.RegNr;
                costToSave.Id = null;
            }
            if (SelectedCost == null)
            {
                dialogResult = MessageBox.Show($"You are about to ADD a new Cost, is this right?\n\n Maintenance: \t {MaintenanceTypTextBox} \n Cost: \t\t {CostTextBox}", "Add New Cost?",
                                               MessageBoxButton.YesNo);
            }
            else
            {
                dialogResult = MessageBox.Show($"You are about to EDIT the selected cost, is this right?\n\n Maintenance:\t {MaintenanceTypTextBox} \n Cost:\t\t {CostTextBox} ", "EDIT cost",
                                               MessageBoxButton.YesNo);
            }
            if (dialogResult == MessageBoxResult.Yes)
            {
                try
                {
                    HttpClient client = new HttpClient();
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Helpers.CredentialHelper.Credential);
                    client.BaseAddress = new Uri("http://moggeapi.azurewebsites.net/");
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    HttpResponseMessage response = await client.PostAsJsonAsync("api/maintenance/addmaintenance/", costToSave);

                    if (response.IsSuccessStatusCode)
                    {
                        SavedOrNotLabel = "It Worked!!";
                    }
                    else
                    {
                        SavedOrNotLabel = "Oh No. It didn´t work!";
                    }
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.ToString());
                    throw;
                }
            }
        }