public void ImageExtensionValidation_ShouldReturnTrue() { // Arrange var imageExtension = ".png"; // Act var result = ImageValidator.ImageExtensionValidation(imageExtension); // Assert Assert.True(result); }
public async Task <PhotoToReturnDto> UploadPhoto(int userId, PhotoForCreationDto photoForCreationDto) { var image = photoForCreationDto.File; if (ImageValidator.ImageExtensionValidation(image.FileName) && ImageValidator.ImageSizeValidation(image.Length) && ImageValidator.ImageSignatureValidation(image)) { using (var memoryStream = new MemoryStream()) { await photoForCreationDto.File.CopyToAsync(memoryStream); var photo = _mapper.Map <Photo>(photoForCreationDto); photo.Image = memoryStream.ToArray(); photo.UserId = userId; // Check if photo that is going to be uploaded is main profile photo // If it is, we have to find current main photo, and change it to not be main var currentMainPhoto = await _unitOfWork.Photos.GetMainPhotoForUser(userId); if (photo.IsMain && currentMainPhoto != null) { currentMainPhoto.IsMain = false; } _unitOfWork.Photos.Add(photo); if (await _unitOfWork.Complete()) { return(_mapper.Map <PhotoToReturnDto>(photo)); } } } return(null); }
public async Task <KeyValuePair <bool, string> > CreateGroup(int userId, GroupForCreationDto group) { // Groups and User relationship is based on Membership so // when we add group we need to add new Membership with desired user var user = await _unitOfWork.Users.GetById(userId); if (user == null) { return(new KeyValuePair <bool, string>(false, "User doesn't exist!")); } if (!(user is PowerUser)) { return(new KeyValuePair <bool, string>(false, "Unauthorized")); } PowerUser powerUser = (PowerUser)user; var groupToAdd = _mapper.Map <Group>(group); if (group.Image != null) { if (ImageValidator.ImageExtensionValidation(group.Image.FileName) && ImageValidator.ImageSizeValidation(group.Image.Length) && ImageValidator.ImageSignatureValidation(group.Image)) { using (var memoryStream = new MemoryStream()) { await group.Image.CopyToAsync(memoryStream); groupToAdd.Image = memoryStream.ToArray(); } } else { return(new KeyValuePair <bool, string>(false, "Image cannot be uploaded")); } } // We check if selected location exists // If it doesn't we create a new one with specified country and city var location = await _unitOfWork.Locations.GetByName(groupToAdd.Name); if (location == null) { location = new Location { Name = group.Location, CityId = group.CityId, CountryId = group.CountryId }; _unitOfWork.Locations.Add(location); } groupToAdd.CreatedBy = powerUser; groupToAdd.Location = location; powerUser.NumberOfGroupsCreated += 1; _unitOfWork.Groups.Add(groupToAdd); if (!await _unitOfWork.Complete()) { return(new KeyValuePair <bool, string>(false, "Couldn't create group")); } var membership = new Membership { UserId = powerUser.Id, GroupId = groupToAdd.Id, DateSent = DateTime.Now, Role = Role.Owner, Accepted = true, DateAccepted = DateTime.Now, User = powerUser, Group = groupToAdd, MembershipStatus = MembershipStatus.Accepted }; _unitOfWork.Memberships.Add(membership); if (await _unitOfWork.Complete()) { return(new KeyValuePair <bool, string>(true, "Group created successfully!")); } return(new KeyValuePair <bool, string>(false, "Something went wrong!")); }