示例#1
0
        public async Task <BaseResponse> Get(int year, int month)
        {
            var response = new BaseResponse();

            User currentUser = await _requestDataService.GetCurrentUser();

            if (month < 1 || month > 12)
            {
                response.Infos.Errors.Add("Month has to be between 1 and 12");
                return(response);
            }

            var date = new DateTime(year, month, 1);

            if (date > DateTime.Now)
            {
                response.Infos.Errors.Add("Requested date can't be in the future");
                return(response);
            }

            var bookings = await _bookingRepository.GetBookingsForMonth(date, currentUser.UserId);

            response.Data.Add("bookings", bookings);

            return(response);
        }
        public async Task <BaseResponse> GetCategoriesForUser()
        {
            var response = new BaseResponse();

            User user = await _requestDataService.GetCurrentUser();

            response.Data.Add("categories", await _categoryRepository.SelectCategoriesForUserWithId(user.UserId));

            return(response);
        }
        public async Task <BaseResponse> Create(MonthlyBalanceDto monthlyBalance)
        {
            var response = new BaseResponse();

            User currentUser = await _requestDataService.GetCurrentUser();

            var currentMonthlyBalance = await _monthlyBalanceRepository.GetNewestMonthlyBalance(currentUser.UserId);

            if (currentMonthlyBalance != null && currentMonthlyBalance.ValidUntil > DateTime.Now)
            {
                response.Infos.Errors.Add($"You already have a monthly balance until {currentMonthlyBalance.ValidUntil.ToString("dd.MM.yyyy")}. Please delete it first to set a new one.");
                response.StatusCode = HttpStatusCode.Conflict;
                return(response);
            }

            MonthlyBalanceDtoValidator validator = new MonthlyBalanceDtoValidator();
            var result = validator.Validate(monthlyBalance);

            if (!result.IsValid)
            {
                response.Infos.Errors.AddRange(result.Errors.ToList().Select(error => error.ErrorMessage));
                response.StatusCode = HttpStatusCode.UnprocessableEntity;
                return(response);
            }

            var newMonthlyBalance = new MonthlyBalance
            {
                ValidUntil = monthlyBalance.ValidUntil,
                AvailableMonthlyBalance = monthlyBalance.AvailableMonthlyBalance,
                BalanceUser             = currentUser
            };

            var dbBalance = await _monthlyBalanceRepository.Insert(newMonthlyBalance);

            dbBalance.BalanceUser = null;

            response.Data.Add("balance", dbBalance);

            return(response);
        }