public async Task <Opinion> CreateOpinion(string text, string offerId) { var currentUser = await profileService.GetCurrentUser(); if (currentUser == null) { return(null); } if (opinionValidationService.UserOpinionExists(currentUser, offerId)) { Alertify.Push("Your opinion on this offer already exists", AlertType.Warning); return(null); } var offer = await database.OfferRepository.Get(offerId); if (offer == null) { return(null); } if (offer.CreatorId == currentUser.Id) { Alertify.Push("You are not allowed to create opinion on your own offer", AlertType.Error); return(null); } var opinion = Opinion.Create(text); offer.Opinions.Add(opinion); currentUser.Opinions.Add(opinion); return(await database.Complete() ? opinion : null); }
public async Task <bool> RemovePhoto(string photoId) { var photo = await database.OfferPhotoRepository.Get(photoId); if (photo == null) { return(false); } if (!offerValidationService.ValidateOfferPhotosCount(-1, photo.Offer)) { Alertify.Push("At least one photo is required", AlertType.Warning); return(false); } database.OfferPhotoRepository.Delete(photo); if (!await database.Complete()) { Alertify.Push("Removing photo failed", AlertType.Error); return(false); } filesManager.Delete(photo.Path); return(true); }
public async Task <SignUpResult> SignUp(string email, string password, string username) { string saltedPasswordHash = string.Empty; var passwordSalt = hashGenerator.CreateSalt(); hashGenerator.GenerateHash(password, passwordSalt, out saltedPasswordHash); var user = new UserBuilder() .SetUsername(username) .SetEmail(email) .SetPassword(saltedPasswordHash, passwordSalt) .Build(); database.UserRepository.Add(user); if (await database.Complete()) { var registerToken = Token.Create(TokenType.Register); database.TokenRepository.Add(registerToken); //Logic adding user to USER role if (await database.Complete()) { return(new SignUpResult(registerToken.Code, user)); } Alertify.Push("Creating register token failed", AlertType.Error); return(null); } Alertify.Push("Creating account failed", AlertType.Error); return(null); }
public async Task <User> SignIn(string email, string password) { if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password)) { Alertify.Push("Invalid email address or password", AlertType.Error); return(null); } var user = await database.UserRepository.Find(u => u.Email.ToLower() == email.ToLower()); if (user == null) { Alertify.Push("Invalid email address or password", AlertType.Error); return(null); } if (!user.EmailConfirmed) { Alertify.Push("Account is not confirmed", AlertType.Warning); return(null); } if (hashGenerator.VerifyHash(password, user.PasswordHash, user.PasswordSalt)) { return(user); } Alertify.Push("Invalid email address or password", AlertType.Error); return(null); }
public async Task <bool> ChangePassword(string oldPassword, string newPassword) { var user = await GetCurrentUser(); if (!hashGenerator.VerifyHash(oldPassword, user.PasswordHash, user.PasswordSalt)) { Alertify.Push("Old password is invalid", AlertType.Error); return(false); } string saltedPasswordHash = string.Empty; var passwordSalt = hashGenerator.CreateSalt(); hashGenerator.GenerateHash(newPassword, passwordSalt, out saltedPasswordHash); user.SetPassword(saltedPasswordHash, passwordSalt); return(await database.Complete()); }
public async Task <BookedDate> BookDate(DateTime startDate, DateTime endDate, string offerId) { var offer = await database.OfferRepository.Get(offerId); if (offer == null) { return(null); } var currentUser = await profileService.GetCurrentUser(); if (currentUser.Id == offer.CreatorId) { Alertify.Push("You are owner of this offer", AlertType.Warning); return(null); } if (!bookingValidationService.IsBookingDateAvailable(startDate, endDate, offer)) { Alertify.Push("This date is already booked", AlertType.Warning); return(null); } if (currentUser != null && bookingValidationService.HasUserAnotherBookedDate(currentUser, offer.Id)) { Alertify.Push("You have already booked this offer", AlertType.Warning); return(null); } var bookedDate = BookedDate.Create(startDate, endDate); offer.BookedDates.Add(bookedDate); if (currentUser != null) { currentUser.BookedDates.Add(bookedDate); } return(await database.Complete() ? bookedDate : null); }