public static void Main(string[] args) { var host = CreateHostBuilder(args).Build(); using (var scope = host.Services.CreateScope()) { var services = scope.ServiceProvider; try { using (var context = new PostDbContext( services.GetRequiredService < DbContextOptions < PostDbContext > >())) { PostDbInit.Init(context); } } catch (Exception e) { var logger = services.GetRequiredService <ILogger <Program> >(); logger.LogError(e, "An error occurred seeding the DB."); } } host.Run(); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, PostDbContext dataContext) { // migrate any database changes on startup (includes initial db creation) dataContext.Database.Migrate(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "Angular Blog API"); }); //TODO //app.UseHttpsRedirection(); app.UseRouting(); app.UseCors(LocalhostAllowSpecificOrigins); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }
public int PaginacionCount_Artista(string desc) { using (var modeldb = new PostDbContext()) { return(modeldb.Artista.Count(p => p.NombreArtista.Contains(desc))); } }
public async void CanReadAllPosts() { DbContextOptions <PostDbContext> options = new DbContextOptionsBuilder <PostDbContext>().UseInMemoryDatabase("ReadAllPosts").Options; using (PostDbContext context = new PostDbContext(options)) { PostManager postService = new PostManager(context); Post post = new Post(); post.Author = "John"; post.ImageURL = "test.img"; post.Caption = "Read me!"; await postService.SaveAsync(post); Post postTwo = new Post(); postTwo.Author = "Bob"; postTwo.ImageURL = "new.img"; postTwo.Caption = "Hello world!"; await postService.SaveAsync(postTwo); Post postThree = new Post(); postThree.Author = "Tim"; postThree.ImageURL = "house.img"; postThree.Caption = "Hi!"; await postService.SaveAsync(postThree); var result = await postService.GetPosts(); int count = result.Count; Assert.Equal(3, count); } }
public async void CanReadAllComments() { DbContextOptions <PostDbContext> options = new DbContextOptionsBuilder <PostDbContext>().UseInMemoryDatabase("ReadAllComments").Options; using (PostDbContext context = new PostDbContext(options)) { CommentManager CommentService = new CommentManager(context); Comment message = new Comment(); message.PostID = 1; message.User = "******"; message.UserComment = "Nice post!"; await CommentService.SaveAsync(message); Comment messageTwo = new Comment(); messageTwo.PostID = 1; messageTwo.User = "******"; messageTwo.UserComment = "I agree!"; await CommentService.SaveAsync(messageTwo); Comment messageThree = new Comment(); messageThree.PostID = 2; messageThree.User = "******"; messageThree.UserComment = "Comment belonging to different post"; await CommentService.SaveAsync(messageThree); var result = await CommentService.GetComments(); int count = result.Count; Assert.Equal(3, count); } }
public async void CanUpdatePostTest() { //testing post manger service DbContextOptions <PostDbContext> options = new DbContextOptionsBuilder <PostDbContext>().UseInMemoryDatabase("CreatePost").Options; using (PostDbContext context = new PostDbContext(options)) { //arrange Post post = new Post(); post.ID = 1; post.Title = "Fun"; post.Caption = "in the sun"; post.URL = "www.funinthesun.com"; post.Title = "Freezing"; post.Caption = "in the snow"; //act PostManager postservice = new PostManager(context); await postservice.SaveAsync(post); var result = context.Posts.FirstOrDefault(p => p.ID == p.ID); //assert Assert.Equal(post, result); } }
public int PaginacionCount_Genero(string desc) { using (var modeldb = new PostDbContext()) { return(modeldb.Genero.Count(p => p.NombreGenero.Contains(desc))); } }
public async void CanEditPost() { DbContextOptions <PostDbContext> options = new DbContextOptionsBuilder <PostDbContext>().UseInMemoryDatabase("EditPost").Options; using (PostDbContext context = new PostDbContext(options)) { PostManager service = new PostManager(context); Post post = new Post(); post.ID = 1; post.PosterName = "tester"; post.URL = "test.jpg"; post.Description = "this is a test post"; await service.SaveAsync(post); Post newpost = new Post(); newpost.PosterName = "Poster"; post.PosterName = newpost.PosterName; await service.SaveAsync(post); Post result = await context.Posts.FirstOrDefaultAsync(h => h.ID == post.ID); Assert.Equal("Poster", result.PosterName); } }
public async void CanDeletePost() { DbContextOptions <PostDbContext> options = new DbContextOptionsBuilder <PostDbContext>().UseInMemoryDatabase("DeletePost").Options; using (PostDbContext context = new PostDbContext(options)) { PostManager service = new PostManager(context); Post p1 = new Post(); p1.ID = 1; p1.PosterName = "alley"; p1.Description = "ts1"; p1.URL = "ts.jpg"; await service.SaveAsync(p1); Post p2 = new Post(); p2.ID = 2; p2.PosterName = "blob"; p2.Description = "ts2"; p2.URL = "ts2.jpg"; await service.SaveAsync(p2); await service.DeleleAsync(p1.ID); var result = await context.Posts.FirstOrDefaultAsync(p => p.ID == p1.ID); Assert.Null(result); } }
public static void AdicionarDadosTeste(PostDbContext context) { context.Post.AddRange(GetPostRepository()); context.Author.AddRange(GetAuthorRepository()); context.Comment.AddRange(GetComentRepository()); context.SaveChanges(); }
public async void CanGetPosts() { DbContextOptions <PostDbContext> options = new DbContextOptionsBuilder <PostDbContext>().UseInMemoryDatabase("GetPosts").Options; using (PostDbContext context = new PostDbContext(options)) { Post post = new Post(); post.ID = 1; post.Author = "Clari"; post.Description = "A Description"; post.Image = "xxx"; Post post2 = new Post(); post2.ID = 2; post2.Author = "Nate"; post2.Description = "Another Description"; post2.Image = "xxxx"; PostService postService = new PostService(context); await postService.SaveAsync(post); await postService.SaveAsync(post2); List <Post> posts = new List <Post>(); posts.Add(post); posts.Add(post2); var result = await postService.GetPosts(); Assert.Equal(posts, result); } }
public static void PostDbSeeder(PostDbContext context) { var postTable = context.Set <Post>(); if (!postTable.Any()) { var post1 = new Post( @"Asp.NetCore源码学习[1-1]:配置[Configuration]", @"", @"初始数据", @"", true); post1.SetAuthor(1, "demo"); postTable.Add(post1); var post2 = new Post( @"Asp.NetCore源码学习[1-2]:配置[Configuration]", @"", @"初始数据", @"", true); post2.SetAuthor(1, "demo"); postTable.Add(post1); var post3 = new Post( @"alan发布的博客", @"", @"alan发布的博客", @"", true); post2.SetAuthor(2, "alan"); postTable.Add(post1); postTable.Add(post2); context.SaveChanges(); } context.SaveChanges(); }
public override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); var returnUrl = filterContext.HttpContext.Request.RawUrl; LoggedUserInfo loggedUser = LoggedUserInfo.GetLoggedUserInfo(); if (loggedUser == null) { //if logged user info not avaliable filterContext.Result = new RedirectResult(String.Concat("/login", "?ReturnUrl=", returnUrl)); } else { //if logged user info avaliable PostDbContext db = new PostDbContext(); string username = loggedUser.Username; string passwordHash = loggedUser.PasswordHash; int count = db.Users.Where(x => x.Username == username && x.Password == passwordHash).Count(); if (count != 1) { //if logged user password not match with orginal password HttpContext.Current.Session.Abandon(); filterContext.Result = new RedirectResult(String.Concat("/login", "?ReturnUrl=", returnUrl)); } } }
public async void CanDeletePost() { DbContextOptions <PostDbContext> options = new DbContextOptionsBuilder <PostDbContext>().UseInMemoryDatabase("DeletePost").Options; using (PostDbContext context = new PostDbContext(options)) { PostManager postService = new PostManager(context); Post post = new Post(); post.Author = "John"; post.ImageURL = "test.img"; post.Caption = "Read me!"; await postService.SaveAsync(post); Post postTwo = new Post(); postTwo.Author = "Bob"; postTwo.ImageURL = "new.img"; postTwo.Caption = "Hello world!"; await postService.SaveAsync(postTwo); Post postThree = new Post(); postThree.Author = "Tim"; postThree.ImageURL = "house.img"; postThree.Caption = "Hi!"; await postService.SaveAsync(postThree); await postService.DeleteAsync(postTwo.ID); var result = await context.Posts.FirstOrDefaultAsync(p => p.ID == postTwo.ID); Assert.Null(result); } }
public async Task <IReadOnlyList <Post> > GetPostsAsync([Service] PostDbContext dbContext) { var result = await dbContext.Posts .OrderByDescending(_ => _.CreatedAt) .ToListAsync(); return(result); }
public bool Guardar_Genero(Genero obj) { using (var modeldb = new PostDbContext()) { modeldb.Genero.Add(obj); modeldb.SaveChanges(); return(true); } }
public int NumeroRegistros_Genero() { using (var modeldb = new PostDbContext()) { int numeroRegistro = (from g in modeldb.Genero select g).Count(); return(numeroRegistro); } }
public List <Genero> MostrarDatos_Genero() { using (var modeldb = new PostDbContext()) { var query = (from g in modeldb.Genero select g).OrderBy(x => x.NombreGenero); return(query.ToList()); } }
public int NumeroRegistros_Cancion() { using (var modeldb = new PostDbContext()) { int numeroRegistro = (from c in modeldb.Cancion select c).Count(); return(numeroRegistro); } }
public bool Guardar_Artista(Artista obj) { using (var modeldb = new PostDbContext()) { modeldb.Artista.Add(obj); modeldb.SaveChanges(); return(true); } }
public int NumeroRegistros_Artista() { using (var modeldb = new PostDbContext()) { int numeroRegistro = (from a in modeldb.Artista select a).Count(); return(numeroRegistro); } }
public bool Guardar_Cancion(Cancion obj) { using (var modeldb = new PostDbContext()) { modeldb.Cancion.Add(obj); modeldb.SaveChanges(); return(true); } }
public List <Artista> PaginacionByDesc_Artista(int index, int maxRows, string desc) { using (var modeldb = new PostDbContext()) { var resul = (from oc in modeldb.Artista where oc.NombreArtista.Contains(desc) select oc) .OrderBy(p => p.NombreArtista). Skip((index - 1) * maxRows).Take(maxRows); return(resul.ToList()); } }
public List <Artista> MostrarDatos_Artista() { using (var modeldb = new PostDbContext()) { var query = (from a in modeldb.Artista select a).OrderBy(x => x.NombreArtista); return(query.ToList()); } }
public List <Genero> PaginacionByDesc_Genero(int startIndex, int maxRows, string desc) { using (var modeldb = new PostDbContext()) { var resul = (from oc in modeldb.Genero where oc.NombreGenero.Contains(desc) select oc) .OrderBy(p => p.NombreGenero) .Skip((startIndex - 1) * maxRows).Take(maxRows); return(resul.ToList()); } }
public List <Artista> Buscar_Artista(Artista obj) { using (var modeldb = new PostDbContext()) { var query = (from a in modeldb.Artista where a.NombreArtista.Contains(obj.NombreArtista) select a); return(query.ToList()); } }
public int NumeroRegistros() { using (var modeldb = new PostDbContext()) { int numRegistros = (from t1 in modeldb.Cancion join t2 in modeldb.Artista on t1.CveartistaCancion equals t2.CveArtista join t3 in modeldb.Genero on t1.CvegeneroCancion equals t3.CveGenero select t1.CveCancion).Count(); return(numRegistros); } }
public static List <string> AutoCompletarNombre_Genero(string nombreGenero) { using (var modeldb = new PostDbContext()) { var query = (from g in modeldb.Genero where g.NombreGenero.Contains(nombreGenero) select g.NombreGenero).OrderBy(x => x); return(query.ToList()); } }
public static List <string> AutoCompletarNombre_Artista(string nombreArtista) { using (var modeldb = new PostDbContext()) { var query = (from a in modeldb.Artista where a.NombreArtista.Contains(nombreArtista) select a.NombreArtista).OrderBy(x => x); return(query.ToList()); } }
public List <Genero> Buscar_Genero(Genero obj) { using (var modeldb = new PostDbContext()) { var query = (from a in modeldb.Genero where a.NombreGenero.Contains(obj.NombreGenero) select a); return(query.ToList()); } }
public PostRepository() { _dbContext = new PostDbContext(); }