Пример #1
0
        public bool UpdateItem(CCategoryDto item)
        {
            try
            {
                using (SpendingHelperDBEntities context = new SpendingHelperDBEntities())
                {
                    CCategoryDto category = context.CCategoriesDto.Where(c => c.CategoryID == item.CategoryID).FirstOrDefault();
                    if (category == null)
                    {
                        log.Info("Can't update category because it doesn't exist in database (categoryId = {0})", item.CategoryID);
                        return(false);
                    }

                    category.Title       = item.Title;
                    category.Description = item.Description;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                log.Error(ex, "Some error occure while trying to update category (Categoryid = {1}). Message: {0}", ex.Message, item.CategoryID);
                return(false);
            }

            return(true);
        }
Пример #2
0
        public bool DeleteItemByKey(int key)
        {
            try
            {
                using (SpendingHelperDBEntities context = new SpendingHelperDBEntities())
                {
                    CCategoryDto category = context.CCategoriesDto.Where(c => c.CategoryID == key).FirstOrDefault();
                    if (category == null)
                    {
                        log.Info("Can't delete category because it doesn't exist in database (categoryId = {0})", key);
                        return(false);
                    }

                    context.CCategoriesDto.Remove(category);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                log.Error(ex, "Some error occure while trying to delete category frim DB (CategoryId = {1}). Message: {0}", ex.Message, key);
                return(false);
            }

            return(true);
        }
Пример #3
0
        public bool AddItem(CCategoryDto item)
        {
            try
            {
                using (SpendingHelperDBEntities context = new SpendingHelperDBEntities())
                {
                    context.CCategoriesDto.Add(item);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                log.Error(ex, "Some error occure while trying to add new category into DB. Message: {0}", ex.Message);
                return(false);
            }

            return(true);
        }
Пример #4
0
        public Boolean TyrEditSubCategory(Int32 personId, Int32 subCategoryId, String newCategoryTitle, String newSubCategoryTitle, String newSubCategoryDescription)
        {
            try
            {
                using (SpendingHelperDBEntities context = new SpendingHelperDBEntities())
                {
                    CSubCategoryDto subCategoryDto = context.CSubCategoriesDto.Where(s => s.SubCategoryID == subCategoryId).FirstOrDefault();
                    if (subCategoryDto == null)
                    {
                        log.Warn("Can't find subCategory (id = {0}) to edit)", subCategoryId);
                        return(false);
                    }

                    if (subCategoryDto.PersonID != personId)
                    {
                        log.Warn("Can't edit subCategory because person (id = {0}) not an owner of subCategory (id = {1})", personId, subCategoryId);
                        return(false);
                    }

                    if (newCategoryTitle != subCategoryDto.Category.Title)
                    {
                        CCategoryDto newCategoryDto = context.CCategoriesDto.Where(c => c.Title == newCategoryTitle).FirstOrDefault();
                        if (newCategoryDto == null)
                        {
                            log.Warn("Can't find new category (title = {0}) to edit subCategory(id = {1}))", newCategoryTitle, subCategoryId);
                            return(false);
                        }

                        subCategoryDto.ParentCategoryID = newCategoryDto.CategoryID;
                    }

                    subCategoryDto.Title       = newSubCategoryTitle;
                    subCategoryDto.Description = newSubCategoryDescription;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                log.Error(ex, "Some error occure while trying to edit subCategory (id = {1}) into DB. Message: {0}", ex.Message, subCategoryId);
                return(false);
            }

            return(true);
        }
Пример #5
0
        public Int32 TryAddSubCategory(Int32 personId, String categoryTitle, String subCategoryTitle, String subCategoryDescription)
        {
            try
            {
                using (SpendingHelperDBEntities context = new SpendingHelperDBEntities())
                {
                    CPersonDto personDto = context.CPeopleDto
                                           .Include("SubCategories.Category")
                                           .Where(p => p.PersonID == personId)
                                           .FirstOrDefault();
                    if (personDto == null)
                    {
                        log.Info("Can't add payment because person (id = {0}) doesn't exist", personId);
                        return(0);
                    }


                    CCategoryDto category = context.CCategoriesDto.Where(c => c.Title.Equals(categoryTitle)).FirstOrDefault();
                    if (category == null)
                    {
                        log.Info("Can't add SubCategory because category (title  = {0}) doesn't exist", categoryTitle);
                        return(0);
                    }

                    CSubCategoryDto subCatDto = new CSubCategoryDto();
                    subCatDto.Person      = personDto;
                    subCatDto.Title       = subCategoryTitle;
                    subCatDto.Description = subCategoryDescription;
                    subCatDto.Category    = category;

                    context.CSubCategoriesDto.Add(subCatDto);
                    context.SaveChanges();

                    return(subCatDto.SubCategoryID);
                }
            }
            catch (Exception ex)
            {
                log.Error(ex, "Some error occure while trying to add subCategory into DB. Message: {0}", ex.Message);
                return(0);
            }
        }