public async Task <HttpResponseMessage> Get(int id = -1)
        {
            if (id != -1)
            {
                DatabaseContext.Data.Type res = await _db.Types.Where(b => b.Id == id && !b.Deleted).FirstOrDefaultAsync();

                if (res == null)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.NoContent, "No Menu Item Type Found With ID"));
                }

                LibBookingService.Dtos.MenuItemType menuItem = CreateMenuItemTypeFromDbMenuItemType(res);

                return(Request.CreateResponse(HttpStatusCode.OK, menuItem));
            }
            else
            {
                IEnumerable <DatabaseContext.Data.Type> res = await _db.Types.Where(b => !b.Deleted).ToListAsync();

                IEnumerable <LibBookingService.Dtos.MenuItemType> menuItemTypes = res.Select(b => CreateMenuItemTypeFromDbMenuItemType(b)).OrderBy(b => b.Name);

                return(menuItemTypes.Any() ?
                       Request.CreateResponse(HttpStatusCode.OK, menuItemTypes) :
                       Request.CreateErrorResponse(HttpStatusCode.NoContent, "No Menu Item Types"));
            }
        }
        public async Task <HttpResponseMessage> Post(LibBookingService.Dtos.MenuItemType menuItemType)
        {
            try
            {
                DatabaseContext.Data.Type newMenuItem = _db.Types.Add(new DatabaseContext.Data.Type
                {
                    Name = menuItemType.Name
                });
                await _db.SaveChangesAsync();

                return(Request.CreateResponse(HttpStatusCode.OK, CreateMenuItemTypeFromDbMenuItemType(newMenuItem)));
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Failed"));
            }
        }
        public async Task <HttpResponseMessage> Delete(int id)
        {
            try
            {
                DatabaseContext.Data.Type menuItem = await _db.Types.Where(m => m.Id == id).FirstOrDefaultAsync();

                menuItem.Deleted = true;

                _db.SetModified(menuItem);
                await _db.SaveChangesAsync();

                return(Request.CreateResponse(HttpStatusCode.OK, "Success"));
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Failed"));
            }
        }
        public async Task <HttpResponseMessage> Update(int id, LibBookingService.Dtos.MenuItemType menuItemType)
        {
            try
            {
                DatabaseContext.Data.Type mi = await _db.Types.Where(m => m.Id == id).FirstOrDefaultAsync();

                mi.Name = menuItemType.Name;

                _db.SetModified(mi);
                await _db.SaveChangesAsync();

                LibBookingService.Dtos.MenuItemType res = CreateMenuItemTypeFromDbMenuItemType(mi);

                return(Request.CreateResponse(HttpStatusCode.OK, res));
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Failed"));
            }
        }
 /// <summary>
 /// Returns a menu item type model using the database menu item type parameter.
 /// </summary>
 /// <param name="mi"></param>
 /// <returns></returns>
 private LibBookingService.Dtos.MenuItemType CreateMenuItemTypeFromDbMenuItemType(DatabaseContext.Data.Type mi)
 {
     return(new LibBookingService.Dtos.MenuItemType
     {
         Id = mi.Id,
         Name = mi.Name
     });
 }