public IHttpActionResult CreateGame(GamesDto gamesDto) { if (!ModelState.IsValid) { return(BadRequest()); } var game = Mapper.Map <GamesDto, Game>(gamesDto); _context.Games.Add(game); _context.SaveChanges(); gamesDto.Id = game.Id; return(Created(new Uri(Request.RequestUri + "/" + game.Id), gamesDto)); }
public GamesResponseModel GetAllGames() { GamesResponseModel gamesResponseModel = new GamesResponseModel(); gamesResponseModel.Data = new List <GamesDto>(); DataTable dataTable = new DataTable(); SqlConnection conn = new SqlConnection(DbHelper.DbConnectionString); try { SqlCommand command = new SqlCommand(@"dbo.uspGetAllGames", conn); command.CommandType = CommandType.StoredProcedure; conn.Open(); SqlDataAdapter dataAdapter = new SqlDataAdapter(command); dataAdapter.Fill(dataTable); gamesResponseModel.Message = "Success"; gamesResponseModel.StatusCode = 200; foreach (DataRow row in dataTable.Rows) { GamesDto gamesModel = new GamesDto(); gamesModel.GameID = row["GameID"] != DBNull.Value ? Convert.ToInt64(row["GameID"].ToString()) : 0; gamesModel.MainTopicID = row["MainTopicID"] != DBNull.Value ? Convert.ToInt64(row["MainTopicID"].ToString()) : 0; gamesModel.MainTopicNumber = row["MainTopicNumber"] != DBNull.Value ? Convert.ToString(row["MainTopicNumber"]) : string.Empty; gamesModel.GameDescription = row["GameDescription"] != DBNull.Value ? Convert.ToString(row["GameDescription"].ToString()) : string.Empty; gamesResponseModel.Data.Add(gamesModel); } } catch (Exception ex) { gamesResponseModel.StatusCode = 500; gamesResponseModel.Message = ex.Message; gamesResponseModel.Data = null; } finally { dataTable.Clear(); dataTable = null; conn.Close(); } return(gamesResponseModel); }
public IHttpActionResult UpdateGame(GamesDto gamesDto, int id) { if (!ModelState.IsValid) { throw new HttpResponseException(HttpStatusCode.BadRequest); } var gameInDb = _context.Games.SingleOrDefault(g => g.Id == id); if (gameInDb == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } Mapper.Map(gamesDto, gameInDb); _context.SaveChanges(); return(Ok()); }