Exemplo n.º 1
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);
            }
        }
Exemplo n.º 2
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);
            }
        }
Exemplo n.º 3
0
        public HttpResponseMessage Delete(int id)
        {
            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);

                    context.Phones.Remove(phone);
                    return Request.CreateResponse(HttpStatusCode.OK);
                }
            }
            catch (Exception ex)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }
Exemplo n.º 4
0
 public ModelFactory(DBContext context)
 {
     this.context = context;
 }