Exemplo n.º 1
0
        public async Task CheckActivityExistsTestAsync()
        {
            // arrange
            bool retrieved;

            using (var contextScope = ContextFactory.Create())
            {
                var activity = await GetTestActivityAsync();

                // act
                retrieved = await _repository.CheckActivityExistsAsync(activity.ActivityName);
            }

            // assert
            Assert.IsTrue(retrieved, "Activity does not exist");
        }
Exemplo n.º 2
0
        /// <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
                }));
            }
        }