Advertisement
rcarubbi

conexaomongo

May 22nd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.36 KB | None | 0 0
  1. using CoursesAPI.Domain.Entities;
  2. using CoursesAPI.Domain.Repositories;
  3. using MongoDB.Driver;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Threading.Tasks;
  7.  
  8. namespace CoursesAPI.DAL.Repositories
  9. {
  10.     public class CourseRepository : ICourseRepository
  11.     {
  12.         private MongoDB.Driver.IMongoCollection<Course> _context;
  13.  
  14.         public CourseRepository()
  15.         {
  16.             _context = ConnectionManager.GetContext().GetCollection<Course>("courses");
  17.         }
  18.  
  19.         public async Task AddCourseAsync(Course course)
  20.         {
  21.             await _context.InsertOneAsync(course);
  22.            
  23.         }
  24.  
  25.         public async Task DeleteCourseAsync(Guid id)
  26.         {
  27.             await _context.DeleteOneAsync(c => c.Id == id);
  28.         }
  29.  
  30.         public async Task<Course> GetCourseByIdAsync(Guid id)
  31.         {
  32.             var results = await _context.FindAsync(c => c.Id == id);
  33.             return results.SingleOrDefault();
  34.         }
  35.  
  36.         public async Task<IReadOnlyCollection<Course>> GetCoursesAsync()
  37.         {
  38.             var results = await _context.AsQueryable()
  39.                  .ToListAsync();
  40.  
  41.             return results.AsReadOnly();
  42.         }
  43.  
  44.         public async Task UpdateCourseAsync(Course course, Guid id)
  45.         {  
  46.             await _context.ReplaceOneAsync(c => c.Id == id, course);
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement