Exemplo n.º 1
0
        public ActionResult Delete(int id)
        {
            ApiPhone phone = client.GetPhone(id);


            return(View(phone));

            //client.DeletePhone(id);
            //return RedirectToAction("Phones");
        }
Exemplo n.º 2
0
        public static ApiPhone GetPhone(int id)
        {
            var phone = db.Phones.Find(id);

            var phoneApi = new ApiPhone();

            PropertyCopier <Phone, ApiPhone> .Copy(phone, phoneApi, true);

            return(phoneApi);
        }
Exemplo n.º 3
0
 public ActionResult Edit(ApiPhone phone)
 {
     if (client.UpdatePhone(phone))
     {
         ViewBag.message = "Phone Changed";
     }
     else
     {
         ViewBag.message = "Phone not Changed, error";
     }
     return(RedirectToAction("Details", new { id = phone.PhoneId }));
 }
Exemplo n.º 4
0
        public static void PutPhone(int id, ApiPhone ap)
        {
            // Create a new car
            Phone p = db.Phones.Find(id);

            // Copy car
            if (ap.PhoneId == id)
            {
                PropertyCopier <ApiPhone, Phone> .Copy(ap, p, true);
            }
            db.Entry(p).State = System.Data.Entity.EntityState.Modified;
            db.SaveChanges();
        }
Exemplo n.º 5
0
        public static void PostPhone(ApiPhone ph)
        {
            // Create a new EF Customer.
            Phone p = new Phone();

            // Copy the Simplified customer to the EF customer using the tool I provided.
            PropertyCopier <ApiPhone, Phone> .Copy(ph, p, true);

            // Tell EF we want to add the customer.
            db.Phones.Add(p);

            //Save changes
            db.SaveChanges();
        }
Exemplo n.º 6
0
        public HttpResponseMessage PostPhone([FromBody] ApiPhone ph)
        {
            try
            {
                DBAccess.PostPhone(ph);
            }
            catch (Exception e)
            {
                // ERROR
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Cannot create record."));
            }

            // All OK
            return(Request.CreateResponse(HttpStatusCode.OK));
        }
Exemplo n.º 7
0
        public HttpResponseMessage PutPhone(int id, [FromBody] ApiPhone ap)
        {
            try
            {
                // Persist our change.
                DBAccess.PutPhone(id, ap);
            }
            catch (Exception e)
            {
                // ERROR
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Cannot update record: " + e));
            }

            // All OK
            return(Request.CreateResponse(HttpStatusCode.OK));
        }
Exemplo n.º 8
0
        public ActionResult Create(FormCollection form)
        {
            ApiPhone phone = new ApiPhone()
            {
                PhoneId     = Convert.ToInt32(form["PhoneId"]),
                PhoneNumber = form["PhoneNumber"].ToString(),
            };

            if (client.CreatePhone(phone))
            {
                return(RedirectToAction("Phones"));
            }
            else
            {
                return(View(phone));
            }
        }
        public bool UpdatePhone(ApiPhone phone)
        {
            client.BaseAddress = new Uri("http://localhost:81/");

            var putTask = client.PutAsJsonAsync <ApiPhone>("api/Phone/" + phone.PhoneId, phone);

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

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

            var postTask = client.PostAsJsonAsync("api/Phone", phone);

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

            if (result.IsSuccessStatusCode)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 11
0
        public JsonResult <ApiBaseResponse> ChangePhoneNumber([FromUri] Guid userId, string oldPhoneNumber, string newPhoneNumber, string code)
        {
            var result = new ApiBaseResponse();

            try
            {
                var user     = UserManager.FindById(userId);
                var oldPhone = ServicesHost.GetService <IPhonesProvider>().GetByPhoneNumber(oldPhoneNumber);
                var newPhone = ServicesHost.GetService <IPhonesProvider>().GetByPhoneNumber(newPhoneNumber);

                var validCode = UserManager.VerifyUserToken(userId, IdentityUserTokenHelper.GenerateTokenPurpose(IdentityUserTokenHelper.TokenPurpose.Editing, newPhoneNumber), code);

                if (!validCode || user == null || (oldPhone == null || oldPhone.UserId != userId) ||
                    (newPhone != null && newPhone.UserId.HasValue && newPhone.UserId != Guid.Empty))
                {
                    result.Status = Core.Enums.ApiStatusCode.WrongArgumentsOrData;
                    return(Json(result));
                }

                if (newPhone == null)
                {
                    newPhone = new ApiPhone {
                        Number = newPhoneNumber, UserId = user.Id
                    };
                    ServicesHost.GetService <IPhonesProvider>().Save(newPhone);
                }
                else
                {
                    newPhone.UserId = user.Id;
                    ServicesHost.GetService <IPhonesProvider>().Save(newPhone);
                }

                oldPhone.UserId = null;
                ServicesHost.GetService <IPhonesProvider>().Save(oldPhone);
            }
            catch (Exception ex)
            {
                result.Status = Core.Enums.ApiStatusCode.SystemError;
                result.Error  = ex.Message;
            }

            return(Json(result));
        }
Exemplo n.º 12
0
        public Guid Save(ApiPhone model)
        {
            var phoneEntity = UnitOfWork.GetRepository <Phone>().GetById(model.Id);

            if (phoneEntity == null)
            {
                phoneEntity = Mapper.Map <ApiPhone, Phone>(model);

                UnitOfWork.GetRepository <Phone>().Insert(phoneEntity);
            }
            else
            {
                phoneEntity = Mapper.Map(model, phoneEntity);

                UnitOfWork.GetRepository <Phone>().Update(phoneEntity);
            }

            UnitOfWork.SaveChanges();

            return(phoneEntity.Id);
        }
Exemplo n.º 13
0
 public void Delete(ApiPhone model)
 {
     UnitOfWork.GetRepository <Phone>().Delete(model.Id);
     UnitOfWork.SaveChanges();
 }