public void SetUp() { validGuid = Guid.NewGuid(); validDataModel = new ExpenditureDataModel { Amount = 1, Category = new CategoryDataModel { CreationTime = DateTime.Now, Id = validGuid, Name = "TEST" }, CategoryId = validGuid, CreationTime = DateTime.Now, Id = validGuid, UserId = validGuid }; validProxy = new ExpenditureProxy { Amount = 1, Category = new CategoryProxy { Id = validGuid, Name = "TEST" }, CategoryId = validGuid, Id = validGuid, UserId = validGuid }; assembler = new ExpenditureAssembler(); }
/// <summary> /// Edits a expenditure in the database. /// </summary> /// <param name="expenditure">The edited expenditure.</param> /// <returns>The updated expenditure data model.</returns> public async Task <ExpenditureDataModel> EditExpenditure(ExpenditureDataModel expenditure) { using (var context = new DatabaseContext()) { var toEdit = await GetExpenditure(expenditure.Id); if (toEdit == null) { throw new Exception(Expenditure.Error_CouldNotFindExpenditure); } expenditure.CreationTime = toEdit.CreationTime; var category = await categoryRepository.GetOrAdd(expenditure.Category); expenditure.Category = category; expenditure.CategoryId = category.Id; context.Expenditures.Attach(expenditure); context.Entry(expenditure).State = EntityState.Modified; var rows = await context.SaveChangesAsync(); return(rows > 0 ? expenditure : null); } }
/// <summary> /// Converts a expenditure data model to a proxy. /// </summary> /// <param name="model">The model.</param> /// <returns>The proxy.</returns> private static ExpenditureProxy ExpenditureDataModelToProxy(ExpenditureDataModel model) { return(new ExpenditureProxy { Amount = model.Amount, Category = new CategoryProxy { Id = model.Category.Id, Name = model.Category.Name }, CategoryId = model.CategoryId, Id = model.Id, Description = model.Description, DateOccurred = model.DateOccurred, UserId = model.UserId }); }
/// <summary> /// Creates an instance of the <see cref="EditExpenditureResponse" /> class. /// </summary> /// <param name="model">The data model.</param> /// <param name="requestReference">The request reference.</param> /// <returns> /// The response object. /// </returns> public EditExpenditureResponse NewEditExpenditureResponse(ExpenditureDataModel model, Guid requestReference) { if (requestReference.Equals(Guid.Empty)) { throw new ArgumentNullException(nameof(requestReference)); } if (model == null) { throw new ArgumentNullException(nameof(model)); } return(new EditExpenditureResponse { Expenditure = ExpenditureDataModelToProxy(model), RequestReference = requestReference }); }
/// <summary> /// Creates a new instance of the <see cref="GetExpenditureResponse" />class. /// </summary> /// <param name="expenditure">The expenditure.</param> /// <param name="requestReference">The request reference.</param> /// <returns>The response object.</returns> public GetExpenditureResponse NewGetExpenditureResponse(ExpenditureDataModel expenditure, Guid requestReference) { if (requestReference.Equals(Guid.Empty)) { throw new ArgumentNullException(nameof(requestReference)); } if (expenditure == null) { throw new ArgumentNullException(nameof(expenditure)); } return(new GetExpenditureResponse { RequestReference = requestReference, Expenditure = ExpenditureDataModelToProxy(expenditure) }); }
/// <summary> /// Adds a expenditure to the database. /// </summary> /// <param name="dataModel">The data model.</param> /// <returns> /// The expenditure data model. /// </returns> public async Task <ExpenditureDataModel> AddExpenditure(ExpenditureDataModel dataModel) { using (var context = new DatabaseContext()) { dataModel.Id = Guid.NewGuid(); var category = await categoryRepository.GetOrAdd(dataModel.Category); dataModel.Category = category; dataModel.CategoryId = category.Id; dataModel.CreationTime = DateTime.Now; context.Expenditures.Add(dataModel); var rows = await context.SaveChangesAsync(); return(rows > 0 ? dataModel : null); } }
public void SetUp() { userId = Guid.NewGuid(); expenditureId = Guid.NewGuid(); validData = new List <ExpenditureDataModel>(); invalidDeleteExpenditureRequest = new DeleteExpenditureRequest(); invalidEditExpenditureRequest = new EditExpenditureRequest(); invalidGetExpenditureForUserForMonthRequest = new GetExpenditureForUserForMonthRequest(); invalidGetExpenditureForUserRequest = new GetExpenditureForUserRequest(); invalidGetExpenditureRequest = new GetExpenditureRequest(); invalidAddExpenditureRequest = new AddExpenditureRequest(); validExpenditureDataModel = new ExpenditureDataModel { Amount = 1, Category = new CategoryDataModel { CreationTime = DateTime.Now, Id = Guid.NewGuid(), Name = "TEST" }, CreationTime = DateTime.Now, CategoryId = Guid.NewGuid(), DateOccurred = DateTime.Now, Description = "TEST", Id = Guid.NewGuid(), UserId = Guid.NewGuid() }; expenditureProxy = new ExpenditureProxy { Amount = 1.0, Category = new CategoryProxy { Id = Guid.NewGuid(), Name = "TEST" }, CategoryId = Guid.NewGuid(), DateOccurred = DateTime.Now, Description = "TEST", Id = Guid.NewGuid(), UserId = Guid.NewGuid() }; validDeleteExpenditureRequest = new DeleteExpenditureRequest { ExpenditureId = Guid.NewGuid(), Username = "******" }; validGetExpenditureForUserForMonthRequest = new GetExpenditureForUserForMonthRequest { MonthNumber = 1, UserId = userId, Username = "******" }; validAddExpenditureRequest = new AddExpenditureRequest { Expenditure = expenditureProxy, Username = "******" }; validGetExpenditureForUserRequest = new GetExpenditureForUserRequest { UserId = userId, Username = "******" }; validGetExpenditureRequest = new GetExpenditureRequest { ExpenditureId = expenditureId, Username = "******" }; validEditExpenditureRequest = new EditExpenditureRequest { Expenditure = expenditureProxy, Username = "******" }; validGetExpenditureForUserResponse = new GetExpenditureForUserResponse { Expenditure = new List <ExpenditureProxy> { expenditureProxy } }; validGetExpenditureResponse = new GetExpenditureResponse { Expenditure = expenditureProxy }; validAddExpenditureResponse = new AddExpenditureResponse { Expenditure = expenditureProxy }; validEditExpenditureResponse = new EditExpenditureResponse { Expenditure = expenditureProxy }; validDeleteExpenditureResponse = new DeleteExpenditureResponse { DeleteSuccess = true }; validGetExpenditureForUserForMonthResponse = new GetExpenditureForUserForMonthResponse { Data = new List <ExpenditureProxy> { expenditureProxy } }; assembler = Substitute.For <IExpenditureAssembler>(); repository = Substitute.For <IExpenditureRepository>(); repository.AddExpenditure(validExpenditureDataModel).Returns(validExpenditureDataModel); repository.AddExpenditure(null).Throws(new Exception("TEST")); repository.DeleteExpenditure(validDeleteExpenditureRequest.ExpenditureId).Returns(true); repository.DeleteExpenditure(invalidDeleteExpenditureRequest.ExpenditureId).Throws(new Exception("TEST")); repository.EditExpenditure(validExpenditureDataModel).Returns(validExpenditureDataModel); repository.EditExpenditure(null).Throws(new Exception("TEST")); repository.GetExpenditure(expenditureId).Returns(validExpenditureDataModel); repository.GetExpenditure(Guid.Empty).Throws(new Exception("TEST")); repository.GetExpenditureForUser(validGetExpenditureForUserRequest.UserId) .Returns(new List <ExpenditureDataModel> { validExpenditureDataModel }); repository.GetExpenditureForUser(Guid.Empty).Throws(new Exception("TEST")); repository.GetExpenditureForUserForMonth(userId) .Returns(new List <ExpenditureDataModel> { validExpenditureDataModel }); repository.GetExpenditureForUserForMonth(Guid.Empty).Throws(new Exception("TEST")); assembler.NewAddExpenditureResponse(validExpenditureDataModel, validAddExpenditureRequest.RequestReference) .Returns(validAddExpenditureResponse); assembler.NewDeleteExpenditureResponse(true, validDeleteExpenditureRequest.ExpenditureId) .Returns(validDeleteExpenditureResponse); assembler.NewEditExpenditureResponse( validExpenditureDataModel, validEditExpenditureRequest.RequestReference) .Returns(validEditExpenditureResponse); assembler.NewExpenditureDataModel(expenditureProxy).Returns(validExpenditureDataModel); assembler.NewGetExpenditureForUserForMonthResponse( Arg.Any <List <ExpenditureDataModel> >(), validGetExpenditureForUserForMonthRequest.RequestReference) .Returns(validGetExpenditureForUserForMonthResponse); assembler.NewGetExpenditureForUserResponse( Arg.Any <List <ExpenditureDataModel> >(), validGetExpenditureForUserRequest.RequestReference) .Returns(validGetExpenditureForUserResponse); assembler.NewDeleteExpenditureResponse(true, validDeleteExpenditureRequest.RequestReference) .Returns(validDeleteExpenditureResponse); assembler.NewGetExpenditureResponse(validExpenditureDataModel, validGetExpenditureRequest.RequestReference) .Returns(validGetExpenditureResponse); errorHelper = Substitute.For <IErrorHelper>(); errorHelper.Create(Arg.Any <Exception>(), Arg.Any <string>(), Arg.Any <Type>(), Arg.Any <string>()) .Returns(new ResponseErrorWrapper()); errorHelper.Create(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <Type>(), Arg.Any <string>()) .Returns(new ResponseErrorWrapper()); orchestrator = new ExpenditureOrchestrator(repository, assembler, errorHelper); }