示例#1
0
        public HttpResponseMessage Post(int id, [FromBody] PhoneModel phoneModel)
        {
            try
            {
                if (phoneModel == null)
                    return Request.CreateResponse(HttpStatusCode.BadRequest);

                using (var context = new DBContext())
                {
                    // this should be inject as a dependency
                    var modelFactory = new ModelFactory(context);

                    var phone = modelFactory.Parse(id, phoneModel);
                    if (phone == null)
                        return Request.CreateResponse(HttpStatusCode.BadRequest);

                    // this should be method in Helper
                    context.Phones.Add(phone);
                    context.SaveChanges();

                    return Request.CreateResponse(HttpStatusCode.Created, modelFactory.Create(phone));
                }
            }
            catch (Exception ex)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }
示例#2
0
        public HttpResponseMessage Put(int id, [FromBody] PhoneModel phoneModel)
        {
            try
            {
                using (var context = new DBContext())
                {
                    // this should be inject as a dependency
                    var modelFactory = new ModelFactory(context);

                    // this should be method in Helper
                    var phone = context.Phones.FirstOrDefault(p => p.Id == id);
                    if (phone == null)
                        return Request.CreateResponse(HttpStatusCode.NotFound);

                    var parsedValue = modelFactory.Parse(id, phoneModel);
                    if (parsedValue == null)
                        return Request.CreateResponse(HttpStatusCode.BadRequest);

                    if (phone.Number != parsedValue.Number)
                    {
                        phone.Number = parsedValue.Number;
                        context.SaveChanges();
                        return Request.CreateResponse(HttpStatusCode.OK);
                    }
                    return Request.CreateResponse(HttpStatusCode.BadRequest);
                }
            }
            catch (Exception ex)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }