示例#1
0
        public async Task <List <LocationModel> > GetAll()
        {
            using (var connection = _dataAccessHelper.CreateConnection()) {
                await connection.OpenAsync();

                const string sql  = "select * from Locations";
                var          list = (await connection.QueryAsync <LocationModel>(sql)).ToList();
                return(list);
            }
        }
        public async Task <ReadingModel> CreateReading(ReadingModel reading)
        {
            if (reading.LocationId <= 0)
            {
                throw new ArgumentException("Reading's must have a Location associated with them", "LocationId");
            }
            using (var connection = _dataAccessHelper.CreateConnection()) {
                await connection.OpenAsync();

                const string sql        = "insert into Readings ( LocationId, Day, SolarGenerated, PowerUsed) values (@LocationId, @Day, @SolarGenerated, @PowerUsed); Select LAST_INSERT_ID();";
                var          parameters = new DynamicParameters();
                parameters.Add("@LocationId", reading.LocationId);
                parameters.Add("@Day", reading.Day);
                parameters.Add("@SolarGenerated", reading.SolarGenerated);
                parameters.Add("@PowerUsed", reading.PowerUsed);
                var result = await connection.ExecuteScalarAsync(sql, parameters);

                var id            = Convert.ToInt32(result);
                var readingFromDb = await GetReading(id);

                return(readingFromDb);
            }
        }