示例#1
0
        /// <summary>
        /// Returns all categories related to a specific user
        /// </summary>
        /// <param name="userId">User ID</param>
        /// <param name="includeAmounts">Whether or not include spendings in each category</param>
        /// <param name="fromDate">If includeAmounts it must have a starting date</param>
        /// <param name="toDate">If includeAmounts it must have an ending date</param>
        /// <param name="categoryTypeId">Type of category id</param>
        public List <OCategory> getAll(int userId, bool includeAmounts, string fromDate, string toDate, int?categoryTypeId)
        {
            IQueryable <Category> categoriesModel = context.Categories.AsNoTracking()
                                                    .Include(cat => cat.categoryTypeNavigation)
                                                    .Where(c => c.cUsrId == userId);
            List <OCategory> categories = new List <OCategory>();

            if (categoryTypeId.HasValue)
            {
                categoriesModel.Where(c => c.cTypeId == categoryTypeId.Value);
            }

            foreach (Category cat in categoriesModel.ToList())
            {
                OCategory categoryObject = convert(cat);
                if (includeAmounts)
                {
                    if (fromDate == null || toDate == null)
                    {
                        throw new Exception("Undefined dates");
                    }
                    categoryObject.amount = getAmount(userId, cat.cId.Value, fromDate, toDate);
                }
                categories.Add(categoryObject);
            }

            return(categories);
        }
示例#2
0
        public OCategory getById(int id)
        {
            Category categoryModel = context.Categories.FirstOrDefault(c => c.cId == id);

            if (categoryModel == null)
            {
                throw new Exception($"ERROR: Category with ID [{id}] not found");
            }
            OCategory category = convert(categoryModel);

            return(category);
        }
示例#3
0
        /// <summary>
        /// Transform from Model to Object
        /// </summary>
        private OCategory convert(Category model)
        {
            OCategory category = new OCategory();

            category.id              = model.cId;
            category.name            = model.cName;
            category.userID          = model.cUsrId;
            category.categoryType    = new OCategoryType();
            category.categoryType.id = model.cTypeId;
            if (model.categoryTypeNavigation != null)
            {
                category.categoryType.name = model.categoryTypeNavigation.ctName;
            }

            return(category);
        }
示例#4
0
        public OCategory save(OCategory category)
        {
            // Validations
            if (category == null)
            {
                throw new Exception("ERROR: Category null");
            }
            User user = context.Users.AsNoTracking().FirstOrDefault(u => u.uId == category.userID.Value);

            if (user == null)
            {
                throw new Exception($"ERROR: CATEGORY USER ID [{category.userID}] NOT FOUND. NO SUCH USER");
            }

            // Get model and modify properties
            Category model;

            if (category.id.HasValue)
            {
                model = context.Categories.FirstOrDefault(c => c.cId == category.id.Value);
            }
            else
            {
                model = new Category();
            }
            model.cName   = category.name;
            model.cUsrId  = category.userID.Value;
            model.cTypeId = category.categoryType.id;

            // Update if it's a current category or Add if is a new one
            if (category.id.HasValue)
            {
                context.Categories.Update(model);
            }
            else
            {
                context.Categories.Add(model);
            }
            context.SaveChanges();
            if (!category.id.HasValue)
            {
                category.id = model.cId;
            }

            return(category);
        }
示例#5
0
        public IActionResult EditCategory([FromBody] OCategory category)
        {
            CategoryResponse response = new CategoryResponse();

            response.status         = new Status();
            response.status.success = false;
            try
            {
                using (var transaction = context.Database.BeginTransaction())
                {
                    CategoryManager manager = new CategoryManager(context);
                    response.category       = manager.save(category);
                    response.status.success = true;

                    transaction.Commit();
                }
            }
            catch (Exception exception)
            {
                response.status.errorMessage = exception.Message;
            }
            return(Json(response));
        }