public CategoryDTO(CategoryDTO category)
 {
     CategoryId = category.CategoryId;
     Title = category.Title;
     Group = category.Group;
     Order = category.Order;
 }
 public CategoryEditWindow(CategoryDTO category)
 {
     CEViewModel = new CategoryEditViewModel {OriginalCategory = category};
     InitializeComponent();
     this.DataContext = CEViewModel.EditedCategory;
 }
 public ListResponse<CategoryDTO> GetAllCategories()
 {
     using (var context = new AF_Context())
     {
         try
         {
             List<CategoryDTO> tmp = new List<CategoryDTO>();
             foreach (Category a in context.Categories.OrderBy(c => c.Group).ThenBy(c => c.Order))
             {
                 var newCategoryDto = new CategoryDTO()
                 {
                     CategoryId = a.CategoryId,
                     Title = a.Title,
                     Group = a.Group,
                     Order = a.Order,
                 };
                 tmp.Add(newCategoryDto);
             }
             return (new ListResponse<CategoryDTO>(tmp));
         }
         catch (Exception ex)
         {
             throw;
         }
     }
 }
        public SingleItemResponse<CategoryDTO> GetCategory(int id)
        {
            using (var context = new AF_Context())
            {
                try
                {
                    Category cat = context.Categories.First(c => c.CategoryId == id);
                    //cat.Editor = context.Users.First(u => u.UserId == cat.EditedBy);

                    var newCategoryDto = new CategoryDTO()
                    {
                        CategoryId = cat.CategoryId,
                        Title = cat.Title,
                        Group = cat.Group,
                        Order = cat.Order,
                    };
                    return (new SingleItemResponse<CategoryDTO>(newCategoryDto));
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }
        public SingleItemResponse<CategoryDTO> UpdateCategory(CategoryDTO updateData)
        {
            var updateDataFull = new Category
            {
                CategoryId = updateData.CategoryId,
                Title = updateData.Title,
                EditDate = DateTime.Now,
                EditedBy = GetUserId(),
                Group = updateData.Group,
                Order = updateData.Order
            };

            using (var context = new AF_Context())
            {
                try
                {
                    Category cat = context.Categories.First(c => c.CategoryId == updateData.CategoryId);
                    context.Entry(cat).CurrentValues.SetValues(updateDataFull);
                    context.SaveChanges();
                    int id = updateData.CategoryId;
                    return GetCategory(id);
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }
        public SingleItemResponse<CategoryDTO> AddCategory(CategoryDTO newCategory)
        {
            var newCategoryFull = new Category
            {
                Title = newCategory.Title,
                EditDate = DateTime.Now,
                EditedBy = GetUserId(),
                Group = newCategory.Group,
                Order = newCategory.Order
            };

            using (var context = new AF_Context())
            {
                try
                {
                    context.Categories.Add(newCategoryFull);
                    context.SaveChanges();
                    int id = newCategoryFull.CategoryId;
                    return GetCategory(id);
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }