public HttpResponseMessage GetById(int autoPartId)
        {
            var responseMsg = this.ExceptionHandler(
                 () =>
                 {
                     var context = new AutoMorgueContext();

                     var autoPart = context.AutoParts.Where(p => p.Id == autoPartId).FirstOrDefault();

                     var model = new ReturnedAutoPartModel
                     {
                         Id = autoPart.Id,
                         Name = autoPart.Name,
                         MorgueName = autoPart.Morgue.Name,
                         Price = autoPart.Price,
                         Quantity = autoPart.Quantity,
                         Category = autoPart.Category.Name
                     };

                     var response = this.Request.CreateResponse(HttpStatusCode.OK, model);

                     return response;
                 });

            return responseMsg;
        }
        public HttpResponseMessage UpdateAutoPart(
            [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey, AutoPartUpdateModel model)
        {
            var responseMsg = this.ExceptionHandler(
                 () =>
                 {
                     var context = new AutoMorgueContext();

                     UserDataPersister.ValidateNickname(model.Name);
                     UserDataPersister.ValidateNickname(model.Morgue);
                     UserDataPersister.ValidateNickname(model.Category);

                     var users = context.Users;
                     var user = users.FirstOrDefault(
                           usr => usr.SessionKey == sessionKey);

                     if (user.Role.Name != "admin")
                     {
                         throw new InvalidOperationException("You don't have permission to edit auto parts.");
                     }

                     var curAutoPart = context.AutoParts.Where(a => a.Id == model.Id).FirstOrDefault();

                     if (curAutoPart.Name != model.Name)
                     {
                         curAutoPart.Name = model.Name;
                     }

                     if (curAutoPart.Price != model.Price)
                     {
                         curAutoPart.Price = model.Price;
                     }

                     if (curAutoPart.Quantity != model.Quantity)
                     {
                         curAutoPart.Quantity = model.Quantity;
                     }

                     context.SaveChanges();

                     var autoPartModel = new ReturnedAutoPartModel
                     {
                         Id = curAutoPart.Id,
                         Name = curAutoPart.Name,
                         MorgueName = curAutoPart.Morgue.Name,
                         Price = curAutoPart.Price,
                         Quantity = curAutoPart.Quantity,
                         Category = curAutoPart.Category.Name
                     };

                     var response = this.Request.CreateResponse(HttpStatusCode.OK, autoPartModel);

                     return response;
                 });

            return responseMsg;
        }
        public HttpResponseMessage SaleAutoPart(AutoPartSaleModel model)
        {
            var responseMsg = this.ExceptionHandler(
                 () =>
                 {
                     var context = new AutoMorgueContext();

                     //TODO: Validate Data
                     //UserDataPersister.ValidateUsername(model.Name);
                     //UserDataPersister.ValidateNickname(model.Location);
                     //UserDataPersister.ValidateAuthCode(model.PhoneNumber);
                     var curAutoPart = context.AutoParts.Where(a => a.Id == model.Id).FirstOrDefault();

                     curAutoPart.Quantity = model.Quantity;

                     context.SaveChanges();

                     var autoPartModel = new ReturnedAutoPartModel
                     {
                         Id = curAutoPart.Id,
                         Name = curAutoPart.Name,
                         MorgueName = curAutoPart.Morgue.Name,
                         Price = curAutoPart.Price,
                         Quantity = curAutoPart.Quantity,
                         Category = curAutoPart.Category.Name
                     };

                     var response = this.Request.CreateResponse(HttpStatusCode.OK, autoPartModel);

                     return response;
                 });

            return responseMsg;
        }