예제 #1
0
        public HttpResponseMessage Put(int id, [FromBody] ApiSalesperson newSalesperson)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var entity = db.Salespersons.FirstOrDefault(x => x.SalespersonId == id);

                    if (entity == null)
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.NotFound,
                                                           "Salesperson with Id " + id.ToString() + " not found to update."));
                    }

                    PropertyCopier <ApiSalesperson, Salesperson> .Copy(newSalesperson, entity);

                    db.SaveChanges();

                    return(Request.CreateResponse(HttpStatusCode.OK,
                                                  "Salesperson with Id " + id.ToString() + " found and updated."));
                }
                catch (Exception e)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest,
                                                       "Error in the code"));
                }
            }
            else
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }
        }
예제 #2
0
        public HttpResponseMessage Get()
        {
            try
            {
                List <Salesperson> entities = db.Salespersons.ToList();

                if (entities == null)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.NotFound,
                                                       "No Salespersons Found."));
                }

                List <ApiSalesperson> apiSalespersons = new List <ApiSalesperson>();

                foreach (var record in entities)
                {
                    ApiSalesperson apiSalesperson = new ApiSalesperson();
                    PropertyCopier <Salesperson, ApiSalesperson> .Copy(record, apiSalesperson);

                    apiSalespersons.Add(apiSalesperson);
                }
                return(Request.CreateResponse(HttpStatusCode.OK, apiSalespersons));
            }
            catch (Exception e)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest,
                                                   "Error in the code"));
            }
        }
예제 #3
0
        public bool CreateSalesperson(ApiSalesperson salesperson)
        {
            var client = GetClient();

            client.BaseAddress = new Uri("http://localhost:81/");
            var postTask = client.PostAsJsonAsync("api/Salesperson", salesperson);

            try
            {
                postTask.Wait();
                var result = postTask.Result;
                if (result.IsSuccessStatusCode)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            catch (TaskCanceledException e)
            {
                client.CancelPendingRequests();
                return(false);
            }
        }
예제 #4
0
        public static ApiSalesperson GetSalesperson(int id)
        {
            var salesperson = db.Salespersons.Find(id);

            var salespersonApi = new ApiSalesperson();

            PropertyCopier <Salesperson, ApiSalesperson> .Copy(salesperson, salespersonApi, true);

            return(salespersonApi);
        }
 public ActionResult Edit(ApiSalesperson salesperson)
 {
     if (client.UpdateSalesperson(salesperson))
     {
         ViewBag.message = "Salesperson Changed";
     }
     else
     {
         ViewBag.message = "Salesperson not Changed ??";
     }
     return(RedirectToAction("Salespersons"));
 }
예제 #6
0
        public static void PutSalesperson(int id, ApiSalesperson asp)
        {
            // Create a new car
            Salesperson sp = db.Salespersons.Find(id);

            // Copy car
            if (asp.SalespersonId == id)
            {
                PropertyCopier <ApiSalesperson, Salesperson> .Copy(asp, sp, true);

                db.Entry(sp).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();
            }
        }
예제 #7
0
        public static void PostSalesPerson(ApiSalesperson sp)
        {
            // Create a new EF Customer.
            Salesperson s = new Salesperson();

            // Copy the Simplified customer to the EF customer using the tool I provided.
            PropertyCopier <ApiSalesperson, Salesperson> .Copy(sp, s, true);

            // Tell EF we want to add the customer.
            db.Salespersons.Add(s);

            //Save changes
            db.SaveChanges();
        }
        public HttpResponseMessage PostSalesperson([FromBody] ApiSalesperson sp)
        {
            try
            {
                DBAccess.PostSalesPerson(sp);
            }
            catch (Exception e)
            {
                // ERROR
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Cannot create record."));
            }

            // All OK
            return(Request.CreateResponse(HttpStatusCode.OK));
        }
        public HttpResponseMessage PutSalesperson(int id, [FromBody] ApiSalesperson asp)
        {
            try
            {
                // Persist our change.
                DBAccess.PutSalesperson(id, asp);
            }
            catch (Exception e)
            {
                // ERROR
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Cannot update record: " + e));
            }

            // All OK
            return(Request.CreateResponse(HttpStatusCode.OK));
        }
        public ActionResult Create(FormCollection form)
        {
            ApiSalesperson salesperson = new ApiSalesperson();

            salesperson.SalespersonFirstName   = form["SalespersonFirstName"];
            salesperson.SalespersonLastName    = form["SalespersonLastName"];
            salesperson.SalespersonAddress     = form["SalespersonAddress"];
            salesperson.SalespersonPhoneNumber = form["SalespersonPhoneNumber"];
            salesperson.LocationId             = Int32.Parse(form["Locationid"]);
            if (!client.CreateSalesperson(salesperson))
            {
                return(View(salesperson));
            }
            else
            {
                return(RedirectToAction("Salespersons"));
            }
        }
        public bool UpdateSalesperson(ApiSalesperson salesperson)
        {
            client.BaseAddress = new Uri("http://localhost:81/");

            var putTask = client.PutAsJsonAsync <ApiSalesperson>("api/Salesperson/" + salesperson.SalespersonId, salesperson);

            putTask.Wait();
            var result = putTask.Result;

            if (result.IsSuccessStatusCode)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
        public bool CreateSalesperson(ApiSalesperson salesperson)
        {
            client.BaseAddress = new Uri("http://localhost:81/");

            var postTask = client.PostAsJsonAsync("api/Salesperson", salesperson);

            postTask.Wait();
            var result = postTask.Result;

            if (result.IsSuccessStatusCode)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
예제 #13
0
        public HttpResponseMessage Post([FromBody] ApiSalesperson newSalesperson)
        {
            if (ModelState.IsValid)
            {
                Salesperson c = new Salesperson();
                PropertyCopier <ApiSalesperson, Salesperson> .Copy(newSalesperson, c);

                db.Salespersons.Add(c);
                try
                {
                    db.SaveChanges();
                }
                catch (Exception e)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.NotFound,
                                                       "Cannot add new Salesperson, Try again." + e.StackTrace + "---" + e.InnerException));
                }
            }
            else
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }
            return(Request.CreateResponse(HttpStatusCode.OK, "Salesperson added."));
        }
예제 #14
0
        public HttpResponseMessage Get(int id)
        {
            try
            {
                var entity = db.Salespersons.Find(id);

                if (entity == null)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.NotFound,
                                                       "Salesperson with Id " + id.ToString() + " not found."));
                }

                var myApiSalesperson = new ApiSalesperson();

                PropertyCopier <Salesperson, ApiSalesperson> .Copy(entity, myApiSalesperson);

                return(Request.CreateResponse(HttpStatusCode.OK, myApiSalesperson));
            }
            catch (Exception e)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest,
                                                   "Error in the code"));
            }
        }
        public ActionResult Delete(int id)
        {
            ApiSalesperson salesperson = client.GetSalesperson(id);

            return(View(salesperson));
        }