Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exemplo da aplicação feita com o descritivo dos posts #69

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/.vs
/packages
36 changes: 36 additions & 0 deletions MinutoSeguros.API/APIContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MinutoSeguros.API
{
public class APIContext : DbContext
{
public DbSet<DAO.Post> Posts { get; set; }
public DbSet<DAO.Tag> Tags { get; set; }
public DbSet<DAO.CloudTag> CloudTags { get; set; }
public DbSet<DAO.RestrictedTerm> RestrictedTerms { get; set; }

public APIContext(DbContextOptions<APIContext> options) : base(options){}

protected override void OnModelCreating(ModelBuilder builder)
{

builder.Entity<DAO.Post>().HasKey(x => x.ID);
builder.Entity<DAO.Tag>().HasKey(x => x.ID);
builder.Entity<DAO.RestrictedTerm>().HasKey(x => x.ID);

builder.Entity<DAO.CloudTag>(e => {
e.HasKey(x => x.ID);
e.HasOne(p => p.Post).WithMany(pcts => pcts.CloudTags);
e.HasOne(p => p.Post).WithMany(pcts => pcts.CloudTags);
});

base.OnModelCreating(builder);

}

}
}
42 changes: 42 additions & 0 deletions MinutoSeguros.API/Controllers/PostController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace MinutoSeguros.API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class PostController : ControllerBase
{
private readonly APIContext ctx;
public PostController(APIContext context){ ctx = context; }

[HttpGet]
public async Task<List<DAO.PostDTO>> Get()
{
var mainObjects = await ctx.CloudTags.Include(p => p.Post).Include(t => t.Tag).ToListAsync();
return
(
from item in mainObjects.Select(p => p.Post).Distinct()
select new DAO.PostDTO
{
ID = item.ID,
Link = item.Link,
Title = item.Title,
Counter = item.WordCounter,
Tags = mainObjects
.Where(p => p.PostID == item.ID)
.Select(t => new DAO.TagDTO() {
ID = t.TagID,
Name = t.Tag.Name,
Counter = t.Count
}).ToList()
}
).ToList();
}

}
}
58 changes: 58 additions & 0 deletions MinutoSeguros.API/Controllers/RestrictedTermController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using MinutoSeguros.BLL;

namespace MinutoSeguros.API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class RestrictedTermController : ControllerBase
{
private readonly APIContext ctx;
public RestrictedTermController(APIContext context){ ctx = context; }

[HttpGet]
public async Task<IList<DAO.RestrictedTerm>> Get()
{
return await ctx.RestrictedTerms.ToListAsync();
}

[HttpPost]
public async Task<IActionResult> Post([FromForm] DAO.RestrictedTerm term)
{
if (String.IsNullOrWhiteSpace(term.Name) || String.IsNullOrEmpty(term.Name))
return BadRequest("Your parameters are incorrect");

var objTemp = await ctx.RestrictedTerms.FirstOrDefaultAsync(x => x.Name == term.Name);

if (objTemp != null)
return BadRequest("Item already exists");

ctx.Add(term);

return Ok(await ctx.SaveChangesAsync() >= 1 ? "Item added successfully" : "Item hasn't been added");
}

[HttpDelete("{id}")]
public async Task<IActionResult> Delete(int? id)
{
var currentItem = await ctx.RestrictedTerms.FindAsync(id.Value);

if (currentItem == null)
return NotFound("Item not found");

ctx.RestrictedTerms.Remove(currentItem);

if (await ctx.SaveChangesAsync() >= 1)
return Ok("Item removed successfully");

return UnprocessableEntity("Your item wasn't removed");
}

}
}
74 changes: 74 additions & 0 deletions MinutoSeguros.API/Controllers/SetupController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using MinutoSeguros.BLL;

namespace MinutoSeguros.API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class SetupController : ControllerBase
{
private readonly APIContext ctx;
public SetupController(APIContext context) { ctx = context; }

[HttpGet("one")]
public async Task<bool> GetOne() {
ctx.Posts.RemoveRange(ctx.Posts);
ctx.Tags.RemoveRange(ctx.Tags);
ctx.CloudTags.RemoveRange(ctx.CloudTags);
return (await ctx.SaveChangesAsync() >= 1);
}

[HttpGet("two")]
public async Task<bool> GetTwo() {
var postBusiness = new BLL.PostBusiness();
List<DAO.Post> ldp = await postBusiness.Read();
if (ldp.Count() == 0) return false;
await ctx.Posts.AddRangeAsync(ldp);
return (await ctx.SaveChangesAsync() >= 1);
}

[HttpGet("three")]
public async Task<bool> GetThree() {
var posts = await ctx.Posts.ToListAsync();
var terms = await ctx.RestrictedTerms.ToListAsync();
var restrictedTermBusiness = new RestrictedTermBusiness();
restrictedTermBusiness.CountProcess(ref posts, terms);
return (await ctx.SaveChangesAsync() >= 1);
}

[HttpGet("four")]
public async Task<bool> GetFour() {
var posts = await ctx.Posts.ToListAsync();
var terms = await ctx.RestrictedTerms.ToListAsync();
var cloudTagsDTO = new List<DAO.CloudTagDTO>();
var restrictedTermBusiness = new RestrictedTermBusiness();
restrictedTermBusiness.RankProcess(ref posts, terms, ref cloudTagsDTO);
int countOccurrences = 0;

using (var ctxTrans = ctx.Database.BeginTransaction())
{
var cloudTags = new List<DAO.CloudTag>();

foreach (var ctd in cloudTagsDTO) {
cloudTags.Add(new DAO.CloudTag() {
Tag = new DAO.Tag() { Name = ctd.Tag },
PostID = ctd.PostID,
Count = ctd.Counter
});
}

await ctx.CloudTags.AddRangeAsync(cloudTags);
countOccurrences += await ctx.SaveChangesAsync();
ctxTrans.Commit();
}

return countOccurrences > 0;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading