├── README.md ├── MongoDBGames ├── MongoDBGames │ ├── Model │ │ ├── IGameContext.cs │ │ ├── Settings.cs │ │ ├── Game.cs │ │ └── GameContext.cs │ ├── appsettings.Development.json │ ├── appsettings.json │ ├── Repository │ │ ├── IGameRepository.cs │ │ └── GameRepository.cs │ ├── MongoDBGames.csproj │ ├── Program.cs │ ├── Startup.cs │ └── Controllers │ │ └── GameController.cs └── MongoDBGames.sln └── .gitignore /README.md: -------------------------------------------------------------------------------- 1 | # MongoDB-WebApi -------------------------------------------------------------------------------- /MongoDBGames/MongoDBGames/Model/IGameContext.cs: -------------------------------------------------------------------------------- 1 | using MongoDB.Driver; 2 | 3 | namespace MongoDBGames.Model 4 | { 5 | public interface IGameContext 6 | { 7 | IMongoCollection Games { get; } 8 | } 9 | } -------------------------------------------------------------------------------- /MongoDBGames/MongoDBGames/Model/Settings.cs: -------------------------------------------------------------------------------- 1 | namespace MongoDBGames.Model 2 | { 3 | public class Settings 4 | { 5 | public string ConnectionString { get; set; } 6 | public string Database { get; set; } 7 | } 8 | } -------------------------------------------------------------------------------- /MongoDBGames/MongoDBGames/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Debug", 6 | "System": "Information", 7 | "Microsoft": "Information" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /MongoDBGames/MongoDBGames/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "MongoDB": { 3 | "ConnectionString": "mongodb://localhost:27017", 4 | "Database": "GamesDB" 5 | }, 6 | "Logging": { 7 | "IncludeScopes": false, 8 | "Debug": { 9 | "LogLevel": { 10 | "Default": "Warning" 11 | } 12 | }, 13 | "Console": { 14 | "LogLevel": { 15 | "Default": "Warning" 16 | } 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /MongoDBGames/MongoDBGames/Repository/IGameRepository.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Threading.Tasks; 3 | using MongoDBGames.Model; 4 | 5 | namespace MongoDBGames.Repository 6 | { 7 | public interface IGameRepository 8 | { 9 | Task> GetAllGames(); 10 | Task GetGame(string name); 11 | Task Create(Game game); 12 | Task Update(Game game); 13 | Task Delete(string name); 14 | } 15 | } -------------------------------------------------------------------------------- /MongoDBGames/MongoDBGames/Model/Game.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using MongoDB.Bson; 3 | using MongoDB.Bson.Serialization.Attributes; 4 | 5 | namespace MongoDBGames.Model 6 | { 7 | public class Game 8 | { 9 | [BsonId] 10 | public ObjectId Id { get; set; } 11 | 12 | public string Name { get; set; } 13 | 14 | public string Developer { get; set; } 15 | 16 | public string Publisher { get; set; } 17 | 18 | public List Platforms { get; set; } 19 | } 20 | } -------------------------------------------------------------------------------- /MongoDBGames/MongoDBGames/Model/GameContext.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Extensions.Options; 2 | using MongoDB.Driver; 3 | 4 | namespace MongoDBGames.Model 5 | { 6 | public class GameContext : IGameContext 7 | { 8 | private readonly IMongoDatabase _db; 9 | 10 | public GameContext(IOptions options, IMongoClient client) 11 | { 12 | _db = client.GetDatabase(options.Value.Database); 13 | } 14 | 15 | public IMongoCollection Games => _db.GetCollection("Games"); 16 | } 17 | } -------------------------------------------------------------------------------- /MongoDBGames/MongoDBGames/MongoDBGames.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp2.0 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /MongoDBGames/MongoDBGames/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Threading.Tasks; 6 | using Microsoft.AspNetCore; 7 | using Microsoft.AspNetCore.Hosting; 8 | using Microsoft.Extensions.Configuration; 9 | using Microsoft.Extensions.Logging; 10 | 11 | namespace MongoDBGames 12 | { 13 | public class Program 14 | { 15 | public static void Main(string[] args) 16 | { 17 | BuildWebHost(args).Run(); 18 | } 19 | 20 | public static IWebHost BuildWebHost(string[] args) => 21 | WebHost.CreateDefaultBuilder(args) 22 | .UseStartup() 23 | .Build(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /MongoDBGames/MongoDBGames.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.27130.2027 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MongoDBGames", "MongoDBGames\MongoDBGames.csproj", "{F29CEE0E-00B4-439B-AE2E-13B33A895FA7}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {F29CEE0E-00B4-439B-AE2E-13B33A895FA7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {F29CEE0E-00B4-439B-AE2E-13B33A895FA7}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {F29CEE0E-00B4-439B-AE2E-13B33A895FA7}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {F29CEE0E-00B4-439B-AE2E-13B33A895FA7}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | GlobalSection(ExtensibilityGlobals) = postSolution 23 | SolutionGuid = {537582B9-2A34-4D9E-BD52-26F7FFAF54E9} 24 | EndGlobalSection 25 | EndGlobal 26 | -------------------------------------------------------------------------------- /MongoDBGames/MongoDBGames/Startup.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Builder; 2 | using Microsoft.AspNetCore.Hosting; 3 | using Microsoft.Extensions.Configuration; 4 | using Microsoft.Extensions.DependencyInjection; 5 | using MongoDB.Driver; 6 | using MongoDBGames.Model; 7 | using MongoDBGames.Repository; 8 | 9 | namespace MongoDBGames 10 | { 11 | public class Startup 12 | { 13 | public Startup(IConfiguration configuration) 14 | { 15 | Configuration = configuration; 16 | } 17 | 18 | public IConfiguration Configuration { get; } 19 | 20 | // This method gets called by the runtime. Use this method to add services to the container. 21 | public void ConfigureServices(IServiceCollection services) 22 | { 23 | services.AddMvc(); 24 | 25 | services.Configure( 26 | options => 27 | { 28 | options.ConnectionString = 29 | Configuration.GetSection("MongoDb:ConnectionString").Value; 30 | options.Database = Configuration.GetSection("MongoDb:Database").Value; 31 | }); 32 | 33 | services.AddSingleton( 34 | _ => new MongoClient(Configuration.GetSection("MongoDb:ConnectionString").Value)); 35 | 36 | services.AddTransient(); 37 | services.AddTransient(); 38 | } 39 | 40 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 41 | public void Configure(IApplicationBuilder app, IHostingEnvironment env) 42 | { 43 | if (env.IsDevelopment()) 44 | { 45 | app.UseDeveloperExceptionPage(); 46 | } 47 | 48 | app.UseMvc(); 49 | } 50 | } 51 | } -------------------------------------------------------------------------------- /MongoDBGames/MongoDBGames/Repository/GameRepository.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Threading.Tasks; 3 | using MongoDB.Bson; 4 | using MongoDB.Driver; 5 | using MongoDBGames.Model; 6 | 7 | namespace MongoDBGames.Repository 8 | { 9 | public class GameRepository : IGameRepository 10 | { 11 | private readonly IGameContext _context; 12 | 13 | public GameRepository(IGameContext context) 14 | { 15 | _context = context; 16 | } 17 | 18 | public async Task> GetAllGames() 19 | { 20 | return await _context 21 | .Games 22 | .Find(_ => true) 23 | .ToListAsync(); 24 | } 25 | 26 | public Task GetGame(string name) 27 | { 28 | FilterDefinition filter = Builders.Filter.Eq(m => m.Name, name); 29 | 30 | return _context 31 | .Games 32 | .Find(filter) 33 | .FirstOrDefaultAsync(); 34 | } 35 | 36 | public async Task Create(Game game) 37 | { 38 | await _context.Games.InsertOneAsync(game); 39 | } 40 | 41 | public async Task Update(Game game) 42 | { 43 | ReplaceOneResult updateResult = 44 | await _context 45 | .Games 46 | .ReplaceOneAsync( 47 | filter: g => g.Id == game.Id, 48 | replacement: game); 49 | 50 | return updateResult.IsAcknowledged 51 | && updateResult.ModifiedCount > 0; 52 | } 53 | 54 | public async Task Delete(string name) 55 | { 56 | FilterDefinition filter = Builders.Filter.Eq(m => m.Name, name); 57 | 58 | DeleteResult deleteResult = await _context 59 | .Games 60 | .DeleteOneAsync(filter); 61 | 62 | return deleteResult.IsAcknowledged 63 | && deleteResult.DeletedCount > 0; 64 | } 65 | } 66 | } -------------------------------------------------------------------------------- /MongoDBGames/MongoDBGames/Controllers/GameController.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | using Microsoft.AspNetCore.Mvc; 3 | using MongoDB.Bson; 4 | using MongoDBGames.Model; 5 | using MongoDBGames.Repository; 6 | 7 | namespace MongoDBGames.Controllers 8 | { 9 | [Produces("application/json")] 10 | [Route("api/Game")] 11 | public class GameController : Controller 12 | { 13 | private readonly IGameRepository _gameRepository; 14 | 15 | public GameController(IGameRepository gameRepository) 16 | { 17 | _gameRepository = gameRepository; 18 | } 19 | 20 | // GET: api/Game 21 | [HttpGet] 22 | public async Task Get() 23 | { 24 | return new ObjectResult(await _gameRepository.GetAllGames()); 25 | } 26 | 27 | // GET: api/Game/name 28 | [HttpGet("{name}", Name = "Get")] 29 | public async Task Get(string name) 30 | { 31 | var game = await _gameRepository.GetGame(name); 32 | 33 | if (game == null) 34 | return new NotFoundResult(); 35 | 36 | return new ObjectResult(game); 37 | } 38 | 39 | // POST: api/Game 40 | [HttpPost] 41 | public async Task Post([FromBody]Game game) 42 | { 43 | await _gameRepository.Create(game); 44 | return new OkObjectResult(game); 45 | } 46 | 47 | // PUT: api/Game/5 48 | [HttpPut("{name}")] 49 | public async Task Put(string name, [FromBody]Game game) 50 | { 51 | var gameFromDb = await _gameRepository.GetGame(name); 52 | 53 | if (gameFromDb == null) 54 | return new NotFoundResult(); 55 | 56 | game.Id = gameFromDb.Id; 57 | 58 | await _gameRepository.Update(game); 59 | 60 | return new OkObjectResult(game); 61 | } 62 | 63 | // DELETE: api/ApiWithActions/5 64 | [HttpDelete("{name}")] 65 | public async Task Delete(string name) 66 | { 67 | var gameFromDb = await _gameRepository.GetGame(name); 68 | 69 | if (gameFromDb == null) 70 | return new NotFoundResult(); 71 | 72 | await _gameRepository.Delete(name); 73 | 74 | return new OkResult(); 75 | } 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.suo 8 | *.user 9 | *.userosscache 10 | *.sln.docstates 11 | 12 | # User-specific files (MonoDevelop/Xamarin Studio) 13 | *.userprefs 14 | 15 | # Build results 16 | [Dd]ebug/ 17 | [Dd]ebugPublic/ 18 | [Rr]elease/ 19 | [Rr]eleases/ 20 | x64/ 21 | x86/ 22 | bld/ 23 | [Bb]in/ 24 | [Oo]bj/ 25 | [Ll]og/ 26 | 27 | # Visual Studio 2015 cache/options directory 28 | .vs/ 29 | # Uncomment if you have tasks that create the project's static files in wwwroot 30 | #wwwroot/ 31 | 32 | # MSTest test Results 33 | [Tt]est[Rr]esult*/ 34 | [Bb]uild[Ll]og.* 35 | 36 | # NUNIT 37 | *.VisualState.xml 38 | TestResult.xml 39 | 40 | # Build Results of an ATL Project 41 | [Dd]ebugPS/ 42 | [Rr]eleasePS/ 43 | dlldata.c 44 | 45 | # .NET Core 46 | project.lock.json 47 | project.fragment.lock.json 48 | artifacts/ 49 | **/Properties/launchSettings.json 50 | 51 | *_i.c 52 | *_p.c 53 | *_i.h 54 | *.ilk 55 | *.meta 56 | *.obj 57 | *.pch 58 | *.pdb 59 | *.pgc 60 | *.pgd 61 | *.rsp 62 | *.sbr 63 | *.tlb 64 | *.tli 65 | *.tlh 66 | *.tmp 67 | *.tmp_proj 68 | *.log 69 | *.vspscc 70 | *.vssscc 71 | .builds 72 | *.pidb 73 | *.svclog 74 | *.scc 75 | 76 | # Chutzpah Test files 77 | _Chutzpah* 78 | 79 | # Visual C++ cache files 80 | ipch/ 81 | *.aps 82 | *.ncb 83 | *.opendb 84 | *.opensdf 85 | *.sdf 86 | *.cachefile 87 | *.VC.db 88 | *.VC.VC.opendb 89 | 90 | # Visual Studio profiler 91 | *.psess 92 | *.vsp 93 | *.vspx 94 | *.sap 95 | 96 | # TFS 2012 Local Workspace 97 | $tf/ 98 | 99 | # Guidance Automation Toolkit 100 | *.gpState 101 | 102 | # ReSharper is a .NET coding add-in 103 | _ReSharper*/ 104 | *.[Rr]e[Ss]harper 105 | *.DotSettings.user 106 | 107 | # JustCode is a .NET coding add-in 108 | .JustCode 109 | 110 | # TeamCity is a build add-in 111 | _TeamCity* 112 | 113 | # DotCover is a Code Coverage Tool 114 | *.dotCover 115 | 116 | # Visual Studio code coverage results 117 | *.coverage 118 | *.coveragexml 119 | 120 | # NCrunch 121 | _NCrunch_* 122 | .*crunch*.local.xml 123 | nCrunchTemp_* 124 | 125 | # MightyMoose 126 | *.mm.* 127 | AutoTest.Net/ 128 | 129 | # Web workbench (sass) 130 | .sass-cache/ 131 | 132 | # Installshield output folder 133 | [Ee]xpress/ 134 | 135 | # DocProject is a documentation generator add-in 136 | DocProject/buildhelp/ 137 | DocProject/Help/*.HxT 138 | DocProject/Help/*.HxC 139 | DocProject/Help/*.hhc 140 | DocProject/Help/*.hhk 141 | DocProject/Help/*.hhp 142 | DocProject/Help/Html2 143 | DocProject/Help/html 144 | 145 | # Click-Once directory 146 | publish/ 147 | 148 | # Publish Web Output 149 | *.[Pp]ublish.xml 150 | *.azurePubxml 151 | # TODO: Comment the next line if you want to checkin your web deploy settings 152 | # but database connection strings (with potential passwords) will be unencrypted 153 | *.pubxml 154 | *.publishproj 155 | 156 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 157 | # checkin your Azure Web App publish settings, but sensitive information contained 158 | # in these scripts will be unencrypted 159 | PublishScripts/ 160 | 161 | # NuGet Packages 162 | *.nupkg 163 | # The packages folder can be ignored because of Package Restore 164 | **/packages/* 165 | # except build/, which is used as an MSBuild target. 166 | !**/packages/build/ 167 | # Uncomment if necessary however generally it will be regenerated when needed 168 | #!**/packages/repositories.config 169 | # NuGet v3's project.json files produces more ignorable files 170 | *.nuget.props 171 | *.nuget.targets 172 | 173 | # Microsoft Azure Build Output 174 | csx/ 175 | *.build.csdef 176 | 177 | # Microsoft Azure Emulator 178 | ecf/ 179 | rcf/ 180 | 181 | # Windows Store app package directories and files 182 | AppPackages/ 183 | BundleArtifacts/ 184 | Package.StoreAssociation.xml 185 | _pkginfo.txt 186 | 187 | # Visual Studio cache files 188 | # files ending in .cache can be ignored 189 | *.[Cc]ache 190 | # but keep track of directories ending in .cache 191 | !*.[Cc]ache/ 192 | 193 | # Others 194 | ClientBin/ 195 | ~$* 196 | *~ 197 | *.dbmdl 198 | *.dbproj.schemaview 199 | *.jfm 200 | *.pfx 201 | *.publishsettings 202 | orleans.codegen.cs 203 | 204 | # Since there are multiple workflows, uncomment next line to ignore bower_components 205 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 206 | #bower_components/ 207 | 208 | # RIA/Silverlight projects 209 | Generated_Code/ 210 | 211 | # Backup & report files from converting an old project file 212 | # to a newer Visual Studio version. Backup files are not needed, 213 | # because we have git ;-) 214 | _UpgradeReport_Files/ 215 | Backup*/ 216 | UpgradeLog*.XML 217 | UpgradeLog*.htm 218 | 219 | # SQL Server files 220 | *.mdf 221 | *.ldf 222 | *.ndf 223 | 224 | # Business Intelligence projects 225 | *.rdl.data 226 | *.bim.layout 227 | *.bim_*.settings 228 | 229 | # Microsoft Fakes 230 | FakesAssemblies/ 231 | 232 | # GhostDoc plugin setting file 233 | *.GhostDoc.xml 234 | 235 | # Node.js Tools for Visual Studio 236 | .ntvs_analysis.dat 237 | node_modules/ 238 | 239 | # Typescript v1 declaration files 240 | typings/ 241 | 242 | # Visual Studio 6 build log 243 | *.plg 244 | 245 | # Visual Studio 6 workspace options file 246 | *.opt 247 | 248 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 249 | *.vbw 250 | 251 | # Visual Studio LightSwitch build output 252 | **/*.HTMLClient/GeneratedArtifacts 253 | **/*.DesktopClient/GeneratedArtifacts 254 | **/*.DesktopClient/ModelManifest.xml 255 | **/*.Server/GeneratedArtifacts 256 | **/*.Server/ModelManifest.xml 257 | _Pvt_Extensions 258 | 259 | # Paket dependency manager 260 | .paket/paket.exe 261 | paket-files/ 262 | 263 | # FAKE - F# Make 264 | .fake/ 265 | 266 | # JetBrains Rider 267 | .idea/ 268 | *.sln.iml 269 | 270 | # CodeRush 271 | .cr/ 272 | 273 | # Python Tools for Visual Studio (PTVS) 274 | __pycache__/ 275 | *.pyc 276 | 277 | # Cake - Uncomment if you are using it 278 | # tools/** 279 | # !tools/packages.config 280 | 281 | # Telerik's JustMock configuration file 282 | *.jmconfig 283 | 284 | # BizTalk build output 285 | *.btp.cs 286 | *.btm.cs 287 | *.odx.cs 288 | *.xsd.cs 289 | --------------------------------------------------------------------------------