public async Task <FridgeVoteResponses> VoteForProposition(Guid id, string user) { Data.FridgeRecord fridgeRecord = null; using (var context = CreateLazyPhytonsContext()) { fridgeRecord = await context.FreedgeRecords .Include(x => x.FreedgeRecordsVotingPersons) .ThenInclude(x => x.VotingPerson) .AsNoTracking() .FirstOrDefaultAsync(x => x.Id.Equals(id)) .ConfigureAwait(false); } if (fridgeRecord == null) { return(FridgeVoteResponses.Fail); } var votinPerson = fridgeRecord.FreedgeRecordsVotingPersons.Any(x => x.VotingPerson.User.Equals(user, StringComparison.OrdinalIgnoreCase)); if (votinPerson) { return(FridgeVoteResponses.AlreadyVoted); } using (var context = CreateLazyPhytonsContext()) { var dbVotingPerson = context.VotingPersons.Add(new VotingPerson() { User = user }); context.FreedgeRecordsVotingPersons.Add(new FridgeRecordsVotingPersons() { FridgeRecord = fridgeRecord, FreedgeRecordId = fridgeRecord.Id, VotingPerson = dbVotingPerson.Entity, VotingPersonId = dbVotingPerson.Entity.Id }); await context.SaveChangesAsync().ConfigureAwait(false); } return(FridgeVoteResponses.Ok); }
public static IFridgeRecord ToApi(this Data.FridgeRecord obj) { return(new FridgeRecord(obj.Id, obj.Name, obj.FreedgeRecordsVotingPersons.Count)); }