/// <summary>
        ///     Modifies a goal.
        /// </summary>
        /// <param name="goal">The goal.</param>
        /// <param name="username">The username.</param>
        /// <returns>
        ///     The response object.
        /// </returns>
        public async Task <OrchestratorResponseWrapper <GoalViewModel> > EditGoal(GoalViewModel goal, string username)
        {
            var response = new OrchestratorResponseWrapper <GoalViewModel>();

            try
            {
                var request     = assembler.NewEditGoalRequest(goal, username);
                var apiResponse = await dataAccess.EditGoal(request);

                if (!apiResponse.Success)
                {
                    response.AddErrors(apiResponse.Errors);
                }
                else
                {
                    response.Model = assembler.ProxyToViewModel(apiResponse.Goal);
                }

                response.AddWarnings(apiResponse.Warnings);
            }
            catch (Exception ex)
            {
                var err = errorHelper.Create(ex, username, GetType(), "EditGoal");
                response.AddError(err);
            }

            return(response);
        }
        /// <summary>
        ///     Obtains a goal for the given user.
        /// </summary>
        /// <param name="userId">The user identifier.</param>
        /// <param name="username">The username.</param>
        /// <returns>
        ///     The response object.
        /// </returns>
        public async Task <OrchestratorResponseWrapper <ManageGoalsViewModel> > GetGoalsForUser(
            Guid userId,
            string username)
        {
            var response = new OrchestratorResponseWrapper <ManageGoalsViewModel>();

            try
            {
                var request     = assembler.NewGetGoalsForUserRequest(userId, username);
                var apiResponse = await dataAccess.GetGoalsForUser(request);

                if (!apiResponse.Success)
                {
                    response.AddErrors(apiResponse.Errors);
                }
                else
                {
                    response.Model = assembler.NewManageGoalsViewModel(apiResponse.Goals);
                }

                response.AddWarnings(apiResponse.Warnings);
            }
            catch (Exception ex)
            {
                var err = errorHelper.Create(ex, username, GetType(), "GetGoalsForUser");
                response.AddError(err);
            }

            return(response);
        }
        /// <summary>
        ///     Gets the claim for the given user.
        /// </summary>
        /// <param name="model">The log in model.</param>
        /// <returns>If successful, the <see cref="ClaimsIdentity" /> for the user. If not, errors are returned.</returns>
        public async Task <OrchestratorResponseWrapper <ClaimsIdentity> > ValidateUser(LoginViewModel model)
        {
            var response = new OrchestratorResponseWrapper <ClaimsIdentity>();

            try
            {
                var request     = assembler.NewValidateUserRequest(model);
                var apiResponse = await dataAccess.ValidateUser(request);

                if (!apiResponse.Success || !apiResponse.LoginSuccess)
                {
                    response.AddErrors(apiResponse.Errors);

                    return(response);
                }

                response.AddWarnings(apiResponse.Warnings);

                response.Model = assembler.NewClaimsIdentity(apiResponse);
            }
            catch (Exception ex)
            {
                var err = ErrorHelper.Create(ex, model?.EmailAddress, GetType(), "ValidateUser");
                response.AddError(err);
            }

            return(response);
        }
        /// <summary>
        ///     Registers a user.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns>
        ///     The response object.
        /// </returns>
        public async Task <OrchestratorResponseWrapper <bool> > RegisterUser(RegisterViewModel model)
        {
            var response = new OrchestratorResponseWrapper <bool>();

            try
            {
                var request     = assembler.NewRegisterUserRequest(model);
                var apiResponse = await dataAccess.RegisterUser(request);

                if (!apiResponse.Success || !apiResponse.RegisterSuccess)
                {
                    response.AddErrors(apiResponse.Errors);
                }

                response.AddWarnings(apiResponse.Warnings);

                response.Model = apiResponse.RegisterSuccess;
            }
            catch (Exception ex)
            {
                var err = ErrorHelper.Create(ex, model?.EmailAddress, GetType(), "RegisterUser");
                response.AddError(err);
            }

            return(response);
        }
Exemple #5
0
        /// <summary>
        ///     Builds and sends a request to obtain bills for a specific user for a given month from the database.
        /// </summary>
        /// <param name="monthNumber">The month number.</param>
        /// <param name="userId">The user identifier.</param>
        /// <param name="username">The username.</param>
        /// <returns>
        ///     The response object.
        /// </returns>
        public async Task <OrchestratorResponseWrapper <IList <KeyValuePair <DateTime, double> > > > GetBillsForUserForMonth(
            int monthNumber,
            Guid userId,
            string username)
        {
            var response = new OrchestratorResponseWrapper <IList <KeyValuePair <DateTime, double> > >();

            try
            {
                var request     = assembler.NewGetBillsForUserForMonthRequest(monthNumber, userId, username);
                var apiResponse = await dataAccess.GetBillsForUserForMonth(request);

                if (!apiResponse.Success)
                {
                    response.AddErrors(apiResponse.Errors);

                    return(response);
                }

                response.Model = apiResponse.Data;
            }
            catch (Exception ex)
            {
                var err = ErrorHelper.Create(ex, username, GetType(), "GetBillsForUserForMonth");
                response.AddError(err);
            }

            return(response);
        }
Exemple #6
0
        /// <summary>
        ///     Builds and sends a request to add a bill to the database.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="username">The username.</param>
        /// <returns>
        ///     The response object.
        /// </returns>
        public async Task <OrchestratorResponseWrapper <BillViewModel> > AddBill(BillViewModel model, string username)
        {
            var response = new OrchestratorResponseWrapper <BillViewModel>();

            try
            {
                var request     = assembler.NewAddBillRequest(model, username);
                var apiResponse = await dataAccess.AddBill(request);

                if (!apiResponse.Success)
                {
                    response.AddErrors(apiResponse.Errors);

                    return(response);
                }

                response.AddWarnings(apiResponse.Warnings);

                response.Model = assembler.NewBillViewModel(apiResponse);
            }
            catch (Exception ex)
            {
                var err = ErrorHelper.Create(ex, username, GetType(), "AddBill");
                response.AddError(err);
            }

            return(response);
        }
Exemple #7
0
        /// <summary>
        ///     Exports the user's bills to the given type.
        /// </summary>
        /// <param name="exportType">Type of the export.</param>
        /// <param name="username">The username.</param>
        /// <param name="userId">The user identifier.</param>
        /// <returns>
        ///     The response object.
        /// </returns>
        public async Task <OrchestratorResponseWrapper <ExportViewModel> > ExportBills(
            ExportType exportType,
            string username,
            Guid userId)
        {
            var response = new OrchestratorResponseWrapper <ExportViewModel>();

            try
            {
                var request     = assembler.NewGetBillsForUserRequest(userId, username);
                var apiResponse = await dataAccess.GetBillsForUser(request);

                if (!apiResponse.Success)
                {
                    response.AddErrors(apiResponse.Errors);
                    return(response);
                }

                response.AddWarnings(apiResponse.Warnings);

                response.Model = assembler.NewExportViewModel(exportType, apiResponse.Bills);
            }
            catch (Exception ex)
            {
                var err = ErrorHelper.Create(ex, username, GetType(), "ExportBills");
                response.AddError(err);
            }

            return(response);
        }
Exemple #8
0
        /// <summary>
        ///     Builds and sends a request to delete a bill from the database.
        /// </summary>
        /// <param name="billId">The bill Id.</param>
        /// <param name="username">The username.</param>
        /// <returns>
        ///     The response object.
        /// </returns>
        public async Task <OrchestratorResponseWrapper <bool> > DeleteBill(Guid billId, string username)
        {
            var response = new OrchestratorResponseWrapper <bool>();

            try
            {
                var request = assembler.NewDeleteBillRequest(billId, username);

                var apiResponse = await dataAccess.DeleteBill(request);

                if (!apiResponse.Success || !apiResponse.DeleteSuccess)
                {
                    response.AddErrors(apiResponse.Errors);

                    return(response);
                }

                response.AddWarnings(apiResponse.Warnings);

                response.Model = apiResponse.Success;
            }
            catch (Exception ex)
            {
                var err = ErrorHelper.Create(ex, username, GetType(), "DeleteBill");
                response.AddError(err);
            }

            return(response);
        }
        /// <summary>
        ///     Builds and sends a request to obtain a expenditure from the database.
        /// </summary>
        /// <param name="expenditureId">The expenditure Id.</param>
        /// <param name="username">The username.</param>
        /// <returns>
        ///     The response object.
        /// </returns>
        public async Task <OrchestratorResponseWrapper <ExpenditureViewModel> > GetExpenditure(
            Guid expenditureId,
            string username)
        {
            var response = new OrchestratorResponseWrapper <ExpenditureViewModel>();

            try
            {
                var request = assembler.NewGetExpenditureRequest(expenditureId, username);

                var apiResponse = await dataAccess.GetExpenditure(request);

                if (!apiResponse.Success)
                {
                    response.AddErrors(apiResponse.Errors);

                    return(response);
                }

                response.AddWarnings(apiResponse.Warnings);

                response.Model = assembler.NewExpenditureViewModel(apiResponse);
            }
            catch (Exception ex)
            {
                var err = ErrorHelper.Create(ex, username, GetType(), "GetExpenditure");
                response.AddError(err);
            }

            return(response);
        }
        /// <summary>
        ///     Builds and sends an HTTP request for the data required to produce the bill category chart.
        /// </summary>
        /// <param name="userId">The user identifier.</param>
        /// <param name="username">The username.</param>
        /// <returns>
        ///     The response object.
        /// </returns>
        public async Task <OrchestratorResponseWrapper <IList <KeyValuePair <string, int> > > > GetBillCategoryChartData(
            Guid userId,
            string username)
        {
            var response = new OrchestratorResponseWrapper <IList <KeyValuePair <string, int> > >();

            try
            {
                var request     = assembler.NewGetBillCategoryChartDataRequest(userId, username);
                var apiResponse = await dataAccess.GetBillCategoryChartData(request);

                if (!apiResponse.Success)
                {
                    response.AddErrors(apiResponse.Errors);
                    return(response);
                }

                response.Model = apiResponse.Data;
            }
            catch (Exception ex)
            {
                var err = errorHelper.Create(ex, username, GetType(), "GetBillCategoryChartData");
                response.AddError(err);
            }

            return(response);
        }
        /// <summary>
        ///     Builds and sends an HTTP request for the data required to produce the expenditure chart.
        /// </summary>
        /// <param name="month">The month, represented as an integer.</param>
        /// <param name="userId">The user identifier.</param>
        /// <param name="userEmail">The user email.</param>
        /// <returns>
        ///     The response object.
        /// </returns>
        public async Task <OrchestratorResponseWrapper <IList <KeyValuePair <DateTime, double> > > > GetExpenditureChartData(
            int month,
            Guid userId,
            string userEmail)
        {
            var response = new OrchestratorResponseWrapper <IList <KeyValuePair <DateTime, double> > >();

            try
            {
                var request     = assembler.NewGetExpenditureChartDataRequest(month, userId, userEmail);
                var apiResponse = await dataAccess.GetExpenditureChartData(request);

                if (!apiResponse.Success)
                {
                    response.AddErrors(apiResponse.Errors);

                    return(response);
                }

                response.Model = apiResponse.Data;
            }
            catch (Exception ex)
            {
                var err = errorHelper.Create(ex, userEmail, GetType(), "GetExpenditureChartData");
                response.AddError(err);
            }

            return(response);
        }
 /// <summary>
 ///     Creates a JSON response object based on the given response object.
 /// </summary>
 /// <typeparam name="T">The model type.</typeparam>
 /// <param name="response">The response.</param>
 /// <returns>The JSON response.</returns>
 protected static ContentResult JsonResponse <T>(OrchestratorResponseWrapper <T> response)
 {
     return(new ContentResult
     {
         ContentType = "application/json",
         Content = JsonConvert.SerializeObject(
             response,
             new JsonSerializerSettings
         {
             ContractResolver =
                 new
                 CamelCasePropertyNamesContractResolver()
         }),
         ContentEncoding = Encoding.UTF8
     });
 }
        /// <summary>
        ///     Adds model state errors to the JSON response.
        /// </summary>
        /// <param name="state">The model state.</param>
        /// <returns>The <see cref="ContentResult" />.</returns>
        protected ContentResult InvalidModelState(ModelStateDictionary state)
        {
            var response = new OrchestratorResponseWrapper <bool>();

            foreach (var modelState in state.Values)
            {
                foreach (var error in modelState.Errors)
                {
                    var errorWrapper = errorHelper.Create(
                        error.ErrorMessage,
                        UserEmail,
                        GetType(),
                        "InvalidModelState");

                    response.AddError(errorWrapper);
                }
            }

            return(JsonResponse(response));
        }