public async Task<int> CreateEvent(BirthdayPresentEventCreationDataTransferModel model) { if (this.CanCreateEvent(model)) { var creator = await this.users.All().SingleOrDefaultAsync(x => x.UserName.Equals(model.CreatorUsername)); var birthdayGuy = await this.users.All().SingleOrDefaultAsync(x => x.UserName.Equals(model.BirthdayGuyUsername)); var birthdayDate = DateTime.Parse(model.BirthdayDate); if (creator == null || birthdayGuy == null || birthdayDate == null) { return ServicesConstants.DbModelCreationFailed; } var birthdayPresentEvent = new BirthdayPresentEvent() { Creator = creator, BirthdayGuy = birthdayGuy, BirthdayDate = birthdayDate, IsActive = true }; this.birthdayPresentEvents.Add(birthdayPresentEvent); await this.birthdayPresentEvents.SaveChangesAsync(); int id = birthdayPresentEvent.Id; return id != 0 ? id : ServicesConstants.DbModelInsertionFailed; } else { return ServicesConstants.DbModelCreationFailed; } }
private async Task<bool> CanVote(Present present, BirthdayPresentEvent birthdayPresentEvent, User userVoted) { if (!this.VotingDataIsValid(present,birthdayPresentEvent, userVoted)) { return false; } if(!birthdayPresentEvent.IsActive) { return false; } if (birthdayPresentEvent.BirthdayGuy.UserName == userVoted.UserName) { return false; } if(await this.UserAlreadyVoted(birthdayPresentEvent, userVoted)) { return false; } return true; }
private bool CanCancelEvent(BirthdayPresentEvent eventForCancelation, BirthdayPresentEventCancelationDataTransferModel model) { return eventForCancelation != null && eventForCancelation.Creator.UserName.Equals(model.RequestUsername); }
private async Task<bool> UserAlreadyVoted(BirthdayPresentEvent birthdayPresentEvent, User userVoted) { var userAlreadyVoted = await this.votes.All() .AnyAsync( x => x.BirthdayPresentEventId == birthdayPresentEvent.Id && x.UserVotedId.Equals(userVoted.Id)); if (userAlreadyVoted) { return true; } return false; }
private bool VotingDataIsValid(Present present, BirthdayPresentEvent birthdayPresentEvent, User userVoted) { if (present == null || birthdayPresentEvent == null || userVoted == null) { return false; } return true; }