From ebd36796516329275ce4261adcdc580c57326757 Mon Sep 17 00:00:00 2001 From: Ruidy Nemausat Date: Thu, 6 Feb 2020 07:46:29 +0100 Subject: [PATCH] scaffold controllers --- Controllers/FilesController.cs | 110 +++++++++++++++++++++++++++++ Controllers/HistoriesController.cs | 110 +++++++++++++++++++++++++++++ Controllers/NotesController.cs | 110 +++++++++++++++++++++++++++++ Controllers/ProjectsController.cs | 8 +-- Controllers/TicketsController.cs | 110 +++++++++++++++++++++++++++++ Controllers/UsersController.cs | 110 +++++++++++++++++++++++++++++ Models/History.cs | 4 +- Models/{ => Interfaces}/ITask.cs | 2 +- Models/Project.cs | 19 ++--- README.md | 4 ++ Startup.cs | 33 ++++++++- 11 files changed, 599 insertions(+), 21 deletions(-) create mode 100644 Controllers/FilesController.cs create mode 100644 Controllers/HistoriesController.cs create mode 100644 Controllers/NotesController.cs create mode 100644 Controllers/TicketsController.cs create mode 100644 Controllers/UsersController.cs rename Models/{ => Interfaces}/ITask.cs (87%) diff --git a/Controllers/FilesController.cs b/Controllers/FilesController.cs new file mode 100644 index 0000000..22aa086 --- /dev/null +++ b/Controllers/FilesController.cs @@ -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>> GetFiles() + { + return await _context.Files.ToListAsync(); + } + + // GET: api/Files/5 + [HttpGet("{id}")] + public async Task> 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 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> 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> 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); + } + } +} diff --git a/Controllers/HistoriesController.cs b/Controllers/HistoriesController.cs new file mode 100644 index 0000000..85c8d89 --- /dev/null +++ b/Controllers/HistoriesController.cs @@ -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>> GetEdits() + { + return await _context.Edits.ToListAsync(); + } + + // GET: api/Histories/5 + [HttpGet("{id}")] + public async Task> 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 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> 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> 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); + } + } +} diff --git a/Controllers/NotesController.cs b/Controllers/NotesController.cs new file mode 100644 index 0000000..0646e83 --- /dev/null +++ b/Controllers/NotesController.cs @@ -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>> GetNotes() + { + return await _context.Notes.ToListAsync(); + } + + // GET: api/Notes/5 + [HttpGet("{id}")] + public async Task> 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 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> 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> 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); + } + } +} diff --git a/Controllers/ProjectsController.cs b/Controllers/ProjectsController.cs index bc4e2d0..b07c915 100644 --- a/Controllers/ProjectsController.cs +++ b/Controllers/ProjectsController.cs @@ -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 PutProject(int id, Project project) { if (id != project.Id) - { - return BadRequest(); - } + { return BadRequest(); } _context.Entry(project).State = EntityState.Modified; diff --git a/Controllers/TicketsController.cs b/Controllers/TicketsController.cs new file mode 100644 index 0000000..37ae92e --- /dev/null +++ b/Controllers/TicketsController.cs @@ -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>> GetTickets() + { + return await _context.Tickets.ToListAsync(); + } + + // GET: api/Tickets/5 + [HttpGet("{id}")] + public async Task> 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 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> 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> 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); + } + } +} diff --git a/Controllers/UsersController.cs b/Controllers/UsersController.cs new file mode 100644 index 0000000..f76ad4e --- /dev/null +++ b/Controllers/UsersController.cs @@ -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>> GetUsers() + { + return await _context.Users.ToListAsync(); + } + + // GET: api/Users/5 + [HttpGet("{id}")] + public async Task> 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 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> 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> 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); + } + } +} diff --git a/Models/History.cs b/Models/History.cs index 092b96c..44d45cd 100644 --- a/Models/History.cs +++ b/Models/History.cs @@ -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; } } } \ No newline at end of file diff --git a/Models/ITask.cs b/Models/Interfaces/ITask.cs similarity index 87% rename from Models/ITask.cs rename to Models/Interfaces/ITask.cs index f34d13e..9311efe 100644 --- a/Models/ITask.cs +++ b/Models/Interfaces/ITask.cs @@ -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; } diff --git a/Models/Project.cs b/Models/Project.cs index 8bd7e9c..fa54cf9 100644 --- a/Models/Project.cs +++ b/Models/Project.cs @@ -60,16 +60,19 @@ namespace TicketManager.Models { return this.Assignments.Select(a => a.User).ToList(); } - public void AddMembers(User userToAdd) + public void AddMembers(List 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 membersToRemove) { diff --git a/README.md b/README.md index 63a82b7..6049d88 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/Startup.cs b/Startup.cs index 2544d63..9da91b4 100644 --- a/Startup.cs +++ b/Startup.cs @@ -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();