/// <summary> /// Get all tournaments related to user. /// </summary> /// <param name="userId"></param> /// <returns></returns> public async Task <IEnumerable <TournamentContractModel> > GetAllTournamentsForUser(string userId) { try { this.StartTransaction(); // Sql Query to join and get all the nested object data. string getQuery = @"SELECT t.*, s.*,tt.*, tps.* FROM [dbo].[Tournament] t LEFT JOIN Sport s ON (s.SportId=t.SportId) LEFT JOIN TournamentType tt ON (tt.TournamentTypeId=t.TournamentTypeId) LEFT JOIN TournamentPointSystem tps ON (tps.TournamentPointSystemId=t.TournamentPointSystemId) WHERE t.UserId=@Id"; // expression to set all the required nested objects. var result = await GetConnection.QueryAsync <TournamentContractModel, SportContractModel, TournamentTypeContractModel, TournamentPointSystemContractModel, TournamentContractModel>(getQuery, (t, s, tt, tps) => { t.Sport = new SportContractModel(); t.Sport.SportId = s.SportId; t.Sport.Description = s.Description; t.Sport.Name = s.Name; t.TournamentType = new TournamentTypeContractModel(); t.TournamentType.Name = tt.Name; t.TournamentType.TournamentTypeId = tt.TournamentTypeId; t.TournamentType.Description = tt.Description; t.TournamentPointSystem = new TournamentPointSystemContractModel(); t.TournamentPointSystem.TournamentPointSystemId = tps.TournamentPointSystemId; t.TournamentPointSystem.Name = tps.Name; t.TournamentPointSystem.Winpoint = tps.Winpoint; t.TournamentPointSystem.DrawPoint = tps.DrawPoint; t.TournamentPointSystem.LossPoint = tps.LossPoint; return(t); }, new { Id = userId }, splitOn : "TournamentId,SportId,TournamentTypeId,TournamentPointSystemId", commandType : CommandType.Text, transaction : Transaction); this.CommitTransaction(); return(result); } catch (Exception) { this.RollbackTransaction(); //return false; ?? throw; } }
public async Task <IEnumerable <T> > LoadAllAsync <T>(string query) where T : new() { return(await GetConnection.QueryAsync <T>(query)); }