Skip to content

Instantly share code, notes, and snippets.

@LuceCarter
Last active February 28, 2025 15:37
Show Gist options
  • Select an option

  • Save LuceCarter/cde98e1cf7b79d9dc7047e9e9062231f to your computer and use it in GitHub Desktop.

Select an option

Save LuceCarter/cde98e1cf7b79d9dc7047e9e9062231f to your computer and use it in GitHub Desktop.

Revisions

  1. LuceCarter revised this gist Feb 28, 2025. 4 changed files with 266 additions and 0 deletions.
    25 changes: 25 additions & 0 deletions IMovieService.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    public interface IMovieService
    {
    IEnumerable<Movie> GetAllMovies();

    Movie GetMovieById(string id);

    void UpdateMovie(string movieId, Movie movie);

    void DeleteMovie(Movie movieToDelete);

    string AddMovie(Movie movie);
    IEnumerable<Actor> GetAllActors();

    Actor GetActorById(string id);

    void UpdateActor(string id, Actor actor);

    void DeleteActor(Actor actorToDelete);

    string AddActor(Actor actor);

    IEnumerable<Theater> GetAllTheaters();

    IEnumerable<User> GetAllUsers();
    }
    86 changes: 86 additions & 0 deletions MigratedMoviesEFCore.http
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,86 @@
    @MigratedMoviesEFCore_HostAddress = http://localhost:5030

    GET {{MigratedMoviesEFCore_HostAddress}}/movies
    Accept: application/json

    ###

    POST {{MigratedMoviesEFCore_HostAddress}}/movies
    Content-Type: application/json

    {
    "title": "The Shawshank Redemption",
    "year": 1994,
    "released": "1994-09-23T00:00:00Z",
    "rated": "R",
    "runtime": 142,
    "plot": "Two imprisoned men plot an escape",
    "fullplot": "Andy Dufresne is sentenced to life in prison...",
    "poster": "https://www.imdb.com/title/tt0111161/mediaviewer/rm10105600.jpg"
    }
    ###

    GET {{MigratedMoviesEFCore_HostAddress}}/movies/67c1d51f085bd3612e6d5606

    ###

    PUT {{MigratedMoviesEFCore_HostAddress}}/movies/67c1d51f085bd3612e6d5606
    Content-Type: application/json

    {
    "title": "The Shawshank Redemption",
    "year": 1994,
    "released": "1994-09-23T00:00:00Z",
    "rated": "18",
    "runtime": 142,
    "plot": "Two imprisoned men plot an escape",
    "fullplot": "Andy Dufresne is sentenced to life in prison...",
    "poster": "https://www.imdb.com/title/tt0111161/mediaviewer/rm10105600.jpg"
    }

    ###

    DELETE {{MigratedMoviesEFCore_HostAddress}}/movies/67c1d51f085bd3612e6d5606

    ###

    GET {{MigratedMoviesEFCore_HostAddress}}/actors

    ###

    POST {{MigratedMoviesEFCore_HostAddress}}/actors
    Content-Type: application/json

    {
    "name": "Morgan Freeman",
    "dateOfBirth": "1937-06-02",
    "placeOfBirth": "Memphis, Tennessee, USA"
    }

    ###

    GET {{MigratedMoviesEFCore_HostAddress}}/actors/67c1d5d9085bd3612e6d560b

    ###

    PUT {{MigratedMoviesEFCore_HostAddress}}/actors/67c1d5d9085bd3612e6d560b
    Content-Type: application/json

    {
    "name": "Leonardo DiCaprio",
    "dateOfBirth": "1974-11-11T00:00:00.000Z",
    "placeOfBirth": "Los Angeles, California, USA"
    }
    ###

    DELETE {{MigratedMoviesEFCore_HostAddress}}/actors/67c1d5d9085bd3612e6d560b

    ###

    GET {{MigratedMoviesEFCore_HostAddress}}/theaters

    ###

    GET {{MigratedMoviesEFCore_HostAddress}}/users

    ###
    55 changes: 55 additions & 0 deletions MovieService.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    public Movie GetMovieById(string id)
    {
    return _moviesDbContext.Movies.Find(ObjectId.Parse(id));
    }

    public Actor GetActorById(string id)
    {
    return _moviesDbContext.Actors.Find(ObjectId.Parse(id));
    }

    public string AddMovie(Movie movie)
    {
    _moviesDbContext.Movies.Add(movie);
    // Outputting for debugging purposes
    _moviesDbContext.ChangeTracker.DetectChanges();
    Console.WriteLine(_moviesDbContext.ChangeTracker.DebugView.LongView);

    _moviesDbContext.SaveChanges();

    return movie.Id.ToString();
    }

    public string AddActor(Actor actor)
    {
    _moviesDbContext.Actors.Add(actor);
    // Outputting for debugging purposes
    _moviesDbContext.ChangeTracker.DetectChanges();
    Console.WriteLine(_moviesDbContext.ChangeTracker.DebugView.LongView);

    _moviesDbContext.SaveChanges();

    return actor.Id.ToString();
    }

    public void UpdateMovie(string movieId, Movie movie)
    {
    var movieToUpdate = _moviesDbContext.Movies.FirstOrDefault((m => m.Id == ObjectId.Parse(movieId)));
    _moviesDbContext.Movies.Update(movieToUpdate);

    _moviesDbContext.ChangeTracker.DetectChanges();
    Console.WriteLine(_moviesDbContext.ChangeTracker.DebugView.LongView);

    _moviesDbContext.SaveChanges();
    }

    public void UpdateActor(string actorId, Actor actor)
    {
    var actorToUpdate = _moviesDbContext.Actors.FirstOrDefault(a => a.Id == ObjectId.Parse(actorId));
    _moviesDbContext.Actors.Update(actorToUpdate);
    // Outputting for debugging purposes
    _moviesDbContext.ChangeTracker.DetectChanges();
    Console.WriteLine(_moviesDbContext.ChangeTracker.DebugView.LongView);

    _moviesDbContext.SaveChanges();
    }
    100 changes: 100 additions & 0 deletions Program.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,100 @@
    using Microsoft.EntityFrameworkCore;
    using MigratedMoviesEFCore.Models;
    using MigratedMoviesEFCore.Services;

    var builder = WebApplication.CreateBuilder(args);

    // Add services to the container.
    builder.Services.AddOpenApi();

    builder.Services.AddDbContext<MoviesDbContext>(options =>
    options.UseMongoDB(builder.Configuration.GetConnectionString("MongoDBAtlasConnectionString"), "migrated_mflix")
    .EnableSensitiveDataLogging());

    builder.Services.AddScoped<IMovieService, MovieService>();

    builder.Services.AddEndpointsApiExplorer();
    builder.Services.AddSwaggerGen();

    var app = builder.Build();

    // Configure the HTTP request pipeline.
    if (app.Environment.IsDevelopment())
    {
    app.MapOpenApi();
    app.UseSwagger();
    app.UseSwaggerUI();
    }

    app.UseHttpsRedirection();

    #region movies
    app.MapGet("/movies", (IMovieService movieService) =>
    {
    return movieService.GetAllMovies();
    })
    .WithName("GetMovies");

    app.MapGet("/movies/{id}", (IMovieService movieService, string id) =>
    {
    return movieService.GetMovieById(id);
    }).WithName("GetMovieById");

    app.MapPost("/movies", (IMovieService movieService, Movie movie) =>
    {
    string newId = movieService.AddMovie(movie);
    return newId;
    }).WithName("AddMovie");

    app.MapPut("/movies/{id}", (IMovieService movieService, string id, Movie movieToUpdate) =>
    {
    movieService.UpdateMovie(id, movieToUpdate);
    }).WithName("UpdateMovie");

    app.MapDelete("/movies/{id}", (IMovieService movieService, string id) =>
    {
    var movieToDelete = movieService.GetMovieById(id);
    movieService.DeleteMovie(movieToDelete);
    }).WithName("DeleteMovie");
    #endregion

    #region actors
    app.MapGet("/actors", (IMovieService movieService) =>
    {
    return movieService.GetAllActors();
    }).WithName("GetActors");

    app.MapGet("/actors/{id}", (IMovieService movieService, string id) =>
    {
    return movieService.GetActorById(id);
    }).WithName("GetActorById");

    app.MapPost("/actors", (IMovieService movieService, Actor actor) =>
    {
    string newId = movieService.AddActor(actor);
    return newId;
    }).WithName("AddActor");

    app.MapPut("/actors/{id}", (IMovieService movieService, string id, Actor actorToUpdate) =>
    {
    movieService.UpdateActor(id, actorToUpdate);
    }).WithName("UpdateActor");

    app.MapDelete("/actors/{id}", (IMovieService movieService, string id) =>
    {
    var actorToDelete = movieService.GetActorById(id);
    movieService.DeleteActor(actorToDelete);
    }).WithName("DeleteActor");
    #endregion

    app.MapGet("/theaters", (IMovieService movieService) =>
    {
    return movieService.GetAllTheaters();
    }).WithName("GetTheaters");

    app.MapGet("/users", (IMovieService movieService) =>
    {
    return movieService.GetAllUsers();
    }).WithName("GetUsers");

    app.Run();
  2. LuceCarter created this gist Feb 28, 2025.
    20 changes: 20 additions & 0 deletions Actor.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    using MongoDB.Bson;
    using MongoDB.Bson.Serialization.Attributes;
    using MongoDB.EntityFrameworkCore;

    namespace MigratedMoviesEFCore.Models;

    [Collection("actors")]
    public class Actor
    {
    [BsonId]
    [BsonElement("_id")]
    public ObjectId Id { get; set; }

    [BsonElement("name")]
    public required string Name { get; set; }
    [BsonElement("dateOfBirth")]
    public required DateTime DateOfBirth { get; set; }
    [BsonElement("placeOfBirth")]
    public required string PlaceOfBirth { get; set; }
    }
    48 changes: 48 additions & 0 deletions Movie.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    using System.ComponentModel.DataAnnotations;
    using MongoDB.Bson;
    using MongoDB.Bson.Serialization.Attributes;
    using MongoDB.EntityFrameworkCore;

    namespace MigratedMoviesEFCore.Models;

    [Collection("movies")]
    public class Movie
    {
    [BsonId] [BsonElement("_id")] public ObjectId Id { get; set; } = ObjectId.GenerateNewId();

    [BsonElement("title")]
    public required string Title { get; set; }

    [BsonElement("year")]
    public required int Year { get; set; }

    [BsonElement("runtime")]
    public required int Runtime { get; set; }

    [BsonElement("plot")]
    public required string Plot { get; set; }

    [BsonElement("fullplot")]
    public string FullPlot { get; set; }

    [BsonElement("released")]
    public required DateTime Released { get; set; }

    [BsonElement("rated")]
    public required string Rated { get; set; }

    [BsonElement("poster")]
    public required string Poster { get; set; }

    [BsonElement("comments")]
    public List<Comment>? Comments { get; set; } = new List<Comment>();

    }
    public class Comment
    {
    [BsonElement("text")]
    public required string Text { get; set; }

    [BsonElement("commentDate")]
    public required DateTime CommentDate { get; set; }
    }
    1 change: 1 addition & 0 deletions MovieDbContext.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    protected override void OnModelCreating(ModelBuilder modelBuilder){ modelBuilder.Entity<Movie>().HasMany(m => m.Comments).WithOne().IsRequired(false);}
    38 changes: 38 additions & 0 deletions Theater.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    using MongoDB.Bson;
    using MongoDB.Bson.Serialization.Attributes;
    using MongoDB.EntityFrameworkCore;

    namespace MigratedMoviesEFCore.Models;

    [Collection("theaters")]
    public class Theater
    {

    [BsonId]
    [BsonElement("_id")]
    public ObjectId Id { get; set; }

    [BsonElement("theaterName")]
    public required string TheaterName { get; set; }

    [BsonElement("street1")]
    public string? Street1 { get; set; }

    [BsonElement("street2")]
    public string? Street2 { get; set; }

    [BsonElement("city")]
    public string? City { get; set; }

    [BsonElement("state")]
    public string? State { get; set; }

    [BsonElement("zipcode")]
    public string? Zipcode { get; set; }

    [BsonElement("location_lat")]
    public decimal? LocationLat { get; set; }

    [BsonElement("location_lon")]
    public decimal? LocationLon { get; set; }
    }
    25 changes: 25 additions & 0 deletions User.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    using MongoDB.Bson;
    using MongoDB.Bson.Serialization.Attributes;
    using MongoDB.EntityFrameworkCore;

    namespace MigratedMoviesEFCore.Models;

    [Collection("users")]
    public class User
    {
    [BsonId]
    [BsonElement("_id")]
    public ObjectId Id { get; set; }

    [BsonElement("name")]
    public string? Name { get; set; }

    [BsonElement("email")]
    public string? Email { get; set; }

    [BsonElement("password")]
    public string? Password { get; set; }

    [BsonElement("created_at")]
    public DateTime? CreatedAt { get; set; }
    }