Example #1
0
        public ExpenceDTO Create(int userId, CreateExpenceDTO dto)
        {
            if (dto == null)
            {
                throw new ArgumentNullException("dto");
            }

            var category = _categoryRepo.All(ExpenceCategorySpecifications.ByUserAndId(userId, dto.CategoryId)).SingleOrDefault();
            if (category == null)
            {
                throw new ItemNotFoundException(string.Format("Expence category with id \"{0}\" no found for user \"{1}\".", dto.CategoryId, userId));
            }

            var expence =
                new Expence
                {
                    Date = dto.Date,
                    Comments = dto.Description,
                    Total = dto.Total
                };

            category.Expences.Add(expence);

            _uow.Commit();

            _tagManager.TouchTag("expence", "create", category.UserId);

            return
                new ExpenceDTO
                {
                    Id = expence.ExpenceId,
                    Date = expence.Date,
                    CategoryId = expence.ExpenceCategoryId,
                    Category = expence.Category.Name,
                    Description = expence.Comments,
                    Total = expence.Total
                };
        }
        // POST api/expences
        public ExpenceDTO Post(CreateExpenceDTO dto)
        {
            if (ModelState.IsValid)
            {
                var userId = _userService.GetCurrentUserId();
                var resultDto = _expencesService.Create(userId, dto);

                return resultDto;
            }
            else
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }
        }
Example #3
0
        public void Update(int userId, int expenceId, CreateExpenceDTO dto)
        {
            if (dto == null)
            {
                throw new ArgumentNullException("dto");
            }

            var expence = _expenceRepo.All(ExpenceSpecifications.ByUserAndId(userId, expenceId)).FirstOrDefault();
            if (expence == null)
            {
                throw new ItemNotFoundException(string.Format("Expence with id \"{0}\" not found for user \"{1}\"", expenceId, userId));
            }

            var expenceCategory = _categoryRepo.All(ExpenceCategorySpecifications.ByUserAndId(userId, dto.CategoryId)).FirstOrDefault();
            if (expenceCategory == null)
            {
                throw new ItemNotFoundException(string.Format("ExpenceCategory with id \"{0}\" not found for user \"{1}\"", dto.CategoryId, userId));
            }

            expence.Category = expenceCategory;
            expence.Date = dto.Date;
            expence.Comments = dto.Description;
            expence.Total = dto.Total;

            _uow.Commit();

            _tagManager.TouchTag("expence", "udpate", userId);
        }