/// <summary> /// this method validates the activity model on insert /// </summary> /// <param name="model">the model containing the details of the activity</param> /// <returns>a ValidationResults model</returns> public ValidationResults ValidateInsertModel(ActivityInsertModel model) { var returnValue = new ValidationResults(); if (model == null) { returnValue.ValidationErrors.Add(new ValidationError() { FieldName = "model", ErrorDetail = "The model cannot be null." }); } if (model.ActivityName.Length > 50) { returnValue.ValidationErrors.Add(new ValidationError() { FieldName = "ActivityName", ErrorDetail = "The name is too long." }); } if (model.ActivityDescription.Length > 250) { returnValue.ValidationErrors.Add(new ValidationError() { FieldName = "ActivityDescription", ErrorDetail = "The description is too long." }); } return(returnValue); }
public async Task InsertActivityTestAsync() { // arrange ActivityModel retrieved; var model = new ActivityInsertModel() { ActivityName = "First Test Activity", ActivityDescription = "This activity will be awesome!", ActivityDateTime = DateTime.Now.AddDays(40), ActivityImage = "/images/FirstActivity.jpg" }; // act using (var contextScope = ContextFactory.Create()) { var newId = await _repository.InsertActivityAsync(model, new byte[0]); retrieved = await DbContext.QuerySingleOrDefaultAsync <ActivityModel>($"Select * from Activity where ActivityId = {newId}", commandType : CommandType.Text); } // assert Assert.IsTrue(model.ActivityName == retrieved.ActivityName, "Activity Name is not equal"); Assert.IsTrue(model.ActivityDescription == retrieved.ActivityDescription, "Activity Description is not equal"); Assert.IsTrue(model.ActivityDateTime.ToString() == retrieved.ActivityDateTime.ToString(), "Activity DateTime is not equal"); // the following test would need to change since the image has changed from being a varchar to a varbinary //Assert.IsTrue(model.ActivityImage == retrieved.ActivityImage, "Activity Image is not equal"); }
public async Task<ActivityModel> InsertTestActivityAsync(ActivityInsertModel model) { var sql = @$" declare @NewID int; INSERT INTO dbo.Activity ( ActivityName, ActivityDescription, ActivityDateTime, ActivityImage) VALUES ( '{model.ActivityName.Replace("'","''")}',
/// <summary> /// this method inserts a new record into the activity table /// </summary> /// <param name="model">this is the data model containing the field values</param> /// <returns>the Id of the new record</returns> public async Task <int> InsertActivityAsync(ActivityInsertModel model, byte[] fileContent) { var parameters = new DynamicParameters(); parameters.Add("NewId", dbType: DbType.Int32, direction: ParameterDirection.Output); parameters.Add("ActivityName", model.ActivityName, dbType: DbType.String, direction: ParameterDirection.Input); parameters.Add("ActivityDescription", model.ActivityDescription, dbType: DbType.String, direction: ParameterDirection.Input); parameters.Add("ActivityDateTime", model.ActivityDateTime, dbType: DbType.DateTime, direction: ParameterDirection.Input); parameters.Add("ActivityImage", fileContent, dbType: DbType.Binary, direction: ParameterDirection.Input); await DbContext.ExecuteAsync(StoredProcedures.ActivityInsert, parameters, commandType : CommandType.StoredProcedure); return(parameters.Get <int>("NewId")); }
public async Task<ActivityModel> GetTestActivityAsync( string name = "TestActivity", string description = "This is a test activity, it will be fun, I promise.", DateTime date = default(DateTime), string image = "/images/defaultImage.jpg" ) { ActivityInsertModel insertModel = new ActivityInsertModel { ActivityDateTime = date, ActivityDescription = description, ActivityImage = image, ActivityName = name }; return await InsertTestActivityAsync(insertModel); }
/// <summary> /// this method inserts a new activity into the Activity db Table /// </summary> /// <param name="model">the model containing the new activity data</param> /// <returns>the Id of the inserted record</returns> public async Task <ServiceResult <int> > InsertActivityAsync(ActivityInsertModel model) { try { // validate var validationResult = _validators.ValidateInsertModel(model); if (validationResult.IsValid) { using (var dbScope = ContextFactory.Create()) { // check that the activity doesn't already exist if (!(await _repository.CheckActivityExistsAsync(model.ActivityName))) { // use the model's image path to collect the file from the file system var fileInfo = new FileInfo(Path.Combine(Directory.GetCurrentDirectory(), model.ActivityImage)); // convert the file to a byte array for loading into the database var fileContent = await File.ReadAllBytesAsync(fileInfo.FullName); // send the call to the database var activityId = await _repository.InsertActivityAsync(model, fileContent); dbScope.Transaction.Commit(); // delete the file fileInfo.Delete(); return(new ServiceResult <int>(activityId)); } return(new ServiceResult <int>(new ValidationError() { FieldName = "ActivityName", ErrorDetail = "An activity with that name already exists." })); } } return(new ServiceResult <int>(validationResult.ValidationErrors)); } catch (Exception x) { return(new ServiceResult <int>(new ServiceError() { Location = "InsertActivityAsync", Exception = x.Message })); } }
public async Task <ActionResult> InsertActivityAsync(ActivityInsertModel activity) { try { _logger.LogInformation("InsertActivityAsync executing..."); var srvResult = await _service.InsertActivityAsync(activity); if (srvResult.IsSuccessful) { return(new OkObjectResult(srvResult.Payload)); } if (srvResult.IsException) { return(Problem(detail: srvResult.Exception.Exception, title: srvResult.Exception.Location)); } return(new BadRequestObjectResult(srvResult.Errors)); } catch (Exception x) { _logger.LogError(x, "InsertActivityAsync"); return(Problem(detail: x.StackTrace, title: x.Message)); } }