scaffold controllers

This commit is contained in:
Ruidy Nemausat 2020-02-06 07:46:29 +01:00
parent 254fd91d36
commit ebd3679651
11 changed files with 599 additions and 21 deletions

View file

@ -0,0 +1,110 @@
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 TicketManager.Data;
using TicketManager.Models;
namespace TicketManager.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class FilesController : ControllerBase
{
private readonly AppDbContext _context;
public FilesController(AppDbContext context)
{
_context = context;
}
// GET: api/Files
[HttpGet]
public async Task<ActionResult<IEnumerable<File>>> GetFiles()
{
return await _context.Files.ToListAsync();
}
// GET: api/Files/5
[HttpGet("{id}")]
public async Task<ActionResult<File>> GetFile(int id)
{
var file = await _context.Files.FindAsync(id);
if (file == null)
{
return NotFound();
}
return file;
}
// PUT: api/Files/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://aka.ms/RazorPagesCRUD.
[HttpPut("{id}")]
public async Task<IActionResult> PutFile(int id, File file)
{
if (id != file.Id)
{
return BadRequest();
}
_context.Entry(file).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!FileExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// POST: api/Files
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://aka.ms/RazorPagesCRUD.
[HttpPost]
public async Task<ActionResult<File>> PostFile(File file)
{
_context.Files.Add(file);
await _context.SaveChangesAsync();
return CreatedAtAction("GetFile", new { id = file.Id }, file);
}
// DELETE: api/Files/5
[HttpDelete("{id}")]
public async Task<ActionResult<File>> DeleteFile(int id)
{
var file = await _context.Files.FindAsync(id);
if (file == null)
{
return NotFound();
}
_context.Files.Remove(file);
await _context.SaveChangesAsync();
return file;
}
private bool FileExists(int id)
{
return _context.Files.Any(e => e.Id == id);
}
}
}

View file

@ -0,0 +1,110 @@
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 TicketManager.Data;
using TicketManager.Models;
namespace TicketManager.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class HistoriesController : ControllerBase
{
private readonly AppDbContext _context;
public HistoriesController(AppDbContext context)
{
_context = context;
}
// GET: api/Histories
[HttpGet]
public async Task<ActionResult<IEnumerable<History>>> GetEdits()
{
return await _context.Edits.ToListAsync();
}
// GET: api/Histories/5
[HttpGet("{id}")]
public async Task<ActionResult<History>> GetHistory(int id)
{
var history = await _context.Edits.FindAsync(id);
if (history == null)
{
return NotFound();
}
return history;
}
// PUT: api/Histories/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://aka.ms/RazorPagesCRUD.
[HttpPut("{id}")]
public async Task<IActionResult> PutHistory(int id, History history)
{
if (id != history.Id)
{
return BadRequest();
}
_context.Entry(history).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!HistoryExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// POST: api/Histories
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://aka.ms/RazorPagesCRUD.
[HttpPost]
public async Task<ActionResult<History>> PostHistory(History history)
{
_context.Edits.Add(history);
await _context.SaveChangesAsync();
return CreatedAtAction("GetHistory", new { id = history.Id }, history);
}
// DELETE: api/Histories/5
[HttpDelete("{id}")]
public async Task<ActionResult<History>> DeleteHistory(int id)
{
var history = await _context.Edits.FindAsync(id);
if (history == null)
{
return NotFound();
}
_context.Edits.Remove(history);
await _context.SaveChangesAsync();
return history;
}
private bool HistoryExists(int id)
{
return _context.Edits.Any(e => e.Id == id);
}
}
}

View file

@ -0,0 +1,110 @@
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 TicketManager.Data;
using TicketManager.Models;
namespace TicketManager.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class NotesController : ControllerBase
{
private readonly AppDbContext _context;
public NotesController(AppDbContext context)
{
_context = context;
}
// GET: api/Notes
[HttpGet]
public async Task<ActionResult<IEnumerable<Note>>> GetNotes()
{
return await _context.Notes.ToListAsync();
}
// GET: api/Notes/5
[HttpGet("{id}")]
public async Task<ActionResult<Note>> GetNote(int id)
{
var note = await _context.Notes.FindAsync(id);
if (note == null)
{
return NotFound();
}
return note;
}
// PUT: api/Notes/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://aka.ms/RazorPagesCRUD.
[HttpPut("{id}")]
public async Task<IActionResult> PutNote(int id, Note note)
{
if (id != note.Id)
{
return BadRequest();
}
_context.Entry(note).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!NoteExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// POST: api/Notes
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://aka.ms/RazorPagesCRUD.
[HttpPost]
public async Task<ActionResult<Note>> PostNote(Note note)
{
_context.Notes.Add(note);
await _context.SaveChangesAsync();
return CreatedAtAction("GetNote", new { id = note.Id }, note);
}
// DELETE: api/Notes/5
[HttpDelete("{id}")]
public async Task<ActionResult<Note>> DeleteNote(int id)
{
var note = await _context.Notes.FindAsync(id);
if (note == null)
{
return NotFound();
}
_context.Notes.Remove(note);
await _context.SaveChangesAsync();
return note;
}
private bool NoteExists(int id)
{
return _context.Notes.Any(e => e.Id == id);
}
}
}

View file

@ -35,9 +35,7 @@ namespace TicketManager.Controllers
var project = await _context.Projects.FindAsync(id);
if (project == null)
{
return NotFound();
}
{ return NotFound(); }
return project;
}
@ -49,9 +47,7 @@ namespace TicketManager.Controllers
public async Task<IActionResult> PutProject(int id, Project project)
{
if (id != project.Id)
{
return BadRequest();
}
{ return BadRequest(); }
_context.Entry(project).State = EntityState.Modified;

View file

@ -0,0 +1,110 @@
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 TicketManager.Data;
using TicketManager.Models;
namespace TicketManager.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class TicketsController : ControllerBase
{
private readonly AppDbContext _context;
public TicketsController(AppDbContext context)
{
_context = context;
}
// GET: api/Tickets
[HttpGet]
public async Task<ActionResult<IEnumerable<Ticket>>> GetTickets()
{
return await _context.Tickets.ToListAsync();
}
// GET: api/Tickets/5
[HttpGet("{id}")]
public async Task<ActionResult<Ticket>> GetTicket(int id)
{
var ticket = await _context.Tickets.FindAsync(id);
if (ticket == null)
{
return NotFound();
}
return ticket;
}
// PUT: api/Tickets/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://aka.ms/RazorPagesCRUD.
[HttpPut("{id}")]
public async Task<IActionResult> PutTicket(int id, Ticket ticket)
{
if (id != ticket.Id)
{
return BadRequest();
}
_context.Entry(ticket).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!TicketExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// POST: api/Tickets
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://aka.ms/RazorPagesCRUD.
[HttpPost]
public async Task<ActionResult<Ticket>> PostTicket(Ticket ticket)
{
_context.Tickets.Add(ticket);
await _context.SaveChangesAsync();
return CreatedAtAction("GetTicket", new { id = ticket.Id }, ticket);
}
// DELETE: api/Tickets/5
[HttpDelete("{id}")]
public async Task<ActionResult<Ticket>> DeleteTicket(int id)
{
var ticket = await _context.Tickets.FindAsync(id);
if (ticket == null)
{
return NotFound();
}
_context.Tickets.Remove(ticket);
await _context.SaveChangesAsync();
return ticket;
}
private bool TicketExists(int id)
{
return _context.Tickets.Any(e => e.Id == id);
}
}
}

View file

@ -0,0 +1,110 @@
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 TicketManager.Data;
using TicketManager.Models;
namespace TicketManager.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class UsersController : ControllerBase
{
private readonly AppDbContext _context;
public UsersController(AppDbContext context)
{
_context = context;
}
// GET: api/Users
[HttpGet]
public async Task<ActionResult<IEnumerable<User>>> GetUsers()
{
return await _context.Users.ToListAsync();
}
// GET: api/Users/5
[HttpGet("{id}")]
public async Task<ActionResult<User>> GetUser(int id)
{
var user = await _context.Users.FindAsync(id);
if (user == null)
{
return NotFound();
}
return user;
}
// PUT: api/Users/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://aka.ms/RazorPagesCRUD.
[HttpPut("{id}")]
public async Task<IActionResult> PutUser(int id, User user)
{
if (id != user.Id)
{
return BadRequest();
}
_context.Entry(user).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!UserExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// POST: api/Users
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://aka.ms/RazorPagesCRUD.
[HttpPost]
public async Task<ActionResult<User>> PostUser(User user)
{
_context.Users.Add(user);
await _context.SaveChangesAsync();
return CreatedAtAction("GetUser", new { id = user.Id }, user);
}
// DELETE: api/Users/5
[HttpDelete("{id}")]
public async Task<ActionResult<User>> DeleteUser(int id)
{
var user = await _context.Users.FindAsync(id);
if (user == null)
{
return NotFound();
}
_context.Users.Remove(user);
await _context.SaveChangesAsync();
return user;
}
private bool UserExists(int id)
{
return _context.Users.Any(e => e.Id == id);
}
}
}

View file

@ -9,11 +9,9 @@ namespace TicketManager.Models
public string Title { get; set; }
public string Description { get; set; }
public DateTime UpdateDate { get; } = DateTime.Now;
public ActivityType ActivityType { get; set; } = (ActivityType)0;
public ActivityType ActivityType { get; set; } = ActivityType.Undefined;
public User User { get; set; }
public int UserId { get; set; }
// public ITask Task { get; set; }
// public int TaskId { get; set; }
}
}

View file

@ -4,7 +4,7 @@ namespace TicketManager.Models
{
public interface ITask
{
public int Id { get; set; }
int Id { get; set; }
string Title { get; set; }
string Description { get; set; }
DateTime CreatedAt { get; }

View file

@ -60,16 +60,19 @@ namespace TicketManager.Models
{
return this.Assignments.Select(a => a.User).ToList();
}
public void AddMembers(User userToAdd)
public void AddMembers(List<User> usersToAdd)
{
Assignment newAssign = new Assignment()
foreach (var user in usersToAdd)
{
Project = this,
ProjectId = this.Id,
User = userToAdd,
UserId = userToAdd.Id
};
this.Assignments.Add(newAssign);
Assignment newAssign = new Assignment()
{
Project = this,
ProjectId = this.Id,
User = user,
UserId = user.Id
};
this.Assignments.Add(newAssign);
}
}
public void RemoveMembers(List<User> membersToRemove)
{

View file

@ -3,3 +3,7 @@
## Software Requirement System
- [Follow the link](https://docs.google.com/presentation/d/1Gunf5MRJ_KcoFwo0x_vV8YVHnf9l0V8n7BiJGz6p4cI/edit?usp=sharing)
## ToDo
- Write backend tests

View file

@ -12,12 +12,11 @@ using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.SpaServices.ReactDevelopmentServer;
using Microsoft.EntityFrameworkCore;
using TicketManager.Data;
using TicketManager.Models;
using Microsoft.OpenApi.Models;
using System.Reflection;
using System.IO;
using TicketManager.Data;
using TicketManager.Models;
namespace TicketManager
{
@ -40,6 +39,26 @@ namespace TicketManager
{
configuration.RootPath = "client/build";
});
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Version = "v1",
Title = "Ticket Manager API",
Description = "A simple example ASP.NET Core Web API",
Contact = new OpenApiContact
{
Name = "Ruidy Nemausat",
Email = "ruidy.nemausat@gmail.com",
Url = new Uri("https://ruidywebsite.herokuapp.com/"),
}
});
// Set the comments path for the Swagger JSON and UI.
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);
});
}
@ -55,6 +74,14 @@ namespace TicketManager
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Ticket Manager API V1");
});
app.UseHttpsRedirection();
app.UseSpaStaticFiles();