public Photograph BuildRepositoryModel(SubmitSinglePhotoViewModel viewModel, ApplicationUser user, PhotoCategory category)
        {
            Photograph result = new Photograph();

            result.Photographer            = user;
            result.Category                = category;
            result.UserCategoryPhotoNumber = viewModel.PhotoNumber;
            return(BuildRepositoryModel(viewModel, result));
        }
        public async Task <ProcessResult <string> > SubmitPhoto(SubmitSinglePhotoViewModel viewModel)
        {
            ProcessResult <string> result = new ProcessResult <string>();

            ProcessResult <bool> categoryStatus = await CategoryIsInRequiredStatus(viewModel.CategoryId, CategoryStatusCodes.SubmittingPhotos);

            if (!categoryStatus.Success)
            {
                result.AddError(categoryStatus.ErrorMessage);
            }

            if (!viewModel.FileType.ToLower().StartsWith("image"))
            {
                result.AddError("Silly thing. Only an image file type is allowed");
            }

            if (result.Success)
            {
                Photograph photo = await _db.Photographs
                                   .FirstOrDefaultAsync(p =>
                                                        p.Category.CategoryId == viewModel.CategoryId &&
                                                        p.UserCategoryPhotoNumber == viewModel.PhotoNumber &&
                                                        p.Photographer.UserName == User.Identity.Name);

                SubmitSinglePhotoViewModelMapper mapper     = new SubmitSinglePhotoViewModelMapper();
                ProcessResult <bool>             saveResult = null;

                if (photo == null)
                {
                    ApplicationUser user = await _userManager.FindByNameAsync(User.Identity.Name);

                    if (user == null)
                    {
                        result.AddError("Unable to find this user");
                    }

                    PhotoCategory category = await _db.PhotoCategories.FirstOrDefaultAsync(c => c.CategoryId == viewModel.CategoryId);

                    if (category == null)
                    {
                        result.AddError("Unable to find this category");
                    }

                    if (result.Success)
                    {
                        photo      = mapper.BuildRepositoryModel(viewModel, user, category);
                        saveResult = _db.SaveAdditions(photo);
                    }
                }
                else
                {
                    photo      = mapper.BuildRepositoryModel(viewModel, photo);
                    saveResult = _db.SaveUpdates(photo);
                }

                if (result.Success)
                {
                    if (saveResult.Success)
                    {
                        result.SetResult(GetImageString(photo.SmallImage));
                    }
                    else
                    {
                        result.AddError(saveResult.ErrorMessage);
                    }
                }
            }

            return(result);
        }
 public Photograph BuildRepositoryModel(SubmitSinglePhotoViewModel viewModel, Photograph existingPhoto)
 {
     existingPhoto.FileType = viewModel.FileType;
     SetFileContentsProperties(viewModel.FileContents, existingPhoto);
     return(existingPhoto);
 }