Skip to content
This repository has been archived by the owner on Dec 16, 2018. It is now read-only.

feat: organization controller API built. WIP #22

Merged
merged 2 commits into from
Dec 9, 2016
Merged
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
174 changes: 174 additions & 0 deletions Controllers/OrganizationController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.Extensions.Logging;
using Community.Models;
using Community.Models.AccountViewModels;
using Community.Services;
using Community.Data;

namespace Community.Controllers
{
/**
* Class: OrganizationController
* Purpose: API endpoints for managing organizations
* Methods:
* Get() - Get all organizations
* Get(int id) - Get single organization
* GetAllVolunteersForOrganization(int id) - All who have volunteered for an organization
* Create(Organzation organization) - Create a new organization
* Edit(Organization organization) - Edit an existing organization
* Delete(int id) - Disable an existing organization
*/
[Produces("application/json")]
[Route("api/[controller]")]
public class OrganizationController : Controller
{
private readonly UserManager<ApplicationUser> _userManager;
private ApplicationDbContext context;

public OrganizationController(UserManager<ApplicationUser> userManager, ApplicationDbContext ctx)
{
_userManager = userManager;
context = ctx;
}

private Task<ApplicationUser> GetCurrentUserAsync() => _userManager.GetUserAsync(HttpContext.User);

// GET api/organization
[HttpGet]
public IActionResult Get()
{
IQueryable<Organization> organizations = from organization in context.Organization select organization;
return Json(organizations);
}

// GET api/organization/3
[HttpGet("{id}")]
public IActionResult Get([FromRoute] int id)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
try
{
Organization organization = context.Organization.Single(o => o.OrganizationId == id);
if (organization == null)
{
return NotFound();
}
return Ok(organization);
}
catch
{
return NotFound();
}
}

// GET api/organization/3/volunteers
[HttpGet]
[RouteAttribute("{id}/volunteers")]
public async Task<IActionResult> GetAllVolunteersForOrganization([FromQuery]int id)
{
// ApplicationUser -> EventMember -> Event -> Organization
// TODO
// var volunteers = await (
// from user in context.ApplicationUser
// from eventMember in context.EventMember
// from orgEvent in context.Event
// where eventMember.VolunteerId == ApplicationUser.Id
// where
// ).toListAsync();
return Ok();
}

[HttpPost]
// [Authorize]
public async Task<IActionResult> Create([FromBody]Organization organization)
{
ModelState.Remove("organization.Organizer");

if (ModelState.IsValid)
{
var user = await GetCurrentUserAsync();
var userId = _userManager.GetUserIdAsync(user);
var claims = await _userManager.GetClaimsAsync(user);
organization.Organizer = user;

context.Add(organization);

await context.SaveChangesAsync();
return Json(organization);
}
return BadRequest();
}

[HttpPost]
// [Authorize]
public async Task<IActionResult> Edit(Organization organization)
{
Organization originalOrg = await context.Organization.SingleAsync(o => o.OrganizationId == organization.OrganizationId);

if (ModelState.IsValid)
{
originalOrg.OrganizationId = organization.OrganizationId;
originalOrg.Description = organization.Description;
originalOrg.Organizer = organization.Organizer;
originalOrg.IsActive = organization.IsActive;

context.Entry(originalOrg).State = EntityState.Modified;
context.Update(originalOrg);
await context.SaveChangesAsync();

return Json(originalOrg);
}

return BadRequest();
}

[HttpDelete("{id}")]
// [Authorize]
public async Task<IActionResult> Delete([FromRoute]int id)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}

try
{
Organization orgToDelete = await context.Organization.SingleAsync(o => o.OrganizationId == id);

if (orgToDelete == null)
{
return NotFound();
}

context.Organization.Remove(orgToDelete);
}
catch
{
return NotFound();
}

try
{
await context.SaveChangesAsync();
}
catch
{
return Forbid();
}

return new NoContentResult();
}

}
}
41 changes: 41 additions & 0 deletions Data/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,53 @@ public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
{
}

public DbSet<Achievement> Achievement { get; set; }
public DbSet<ApplicationUser> ApplicationUser { get; set; }
public DbSet<Event> Event { get; set; }
public DbSet<EventChatroomMessage> EventChatroomMessage { get; set; }
public DbSet<EventMember> EventMember { get; set; }
public DbSet<Organization> Organization { get; set; }
public DbSet<VolunteerAchievements> VolunteerAchievements { get; set; }


protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);

//modelBuilder.Entity<Product>()
// .Property(b => b.DateCreated)
// .HasDefaultValueSql("strftime('%Y-%m-%d %H:%M:%S')");

builder.Entity<Achievement>()
.Property(a => a.DateCreated)
.HasDefaultValueSql("strftime('%Y-%m-%d %H:%M:%S')");

builder.Entity<ApplicationUser>()
.Property(a => a.DateCreated)
.HasDefaultValueSql("strftime('%Y-%m-%d %H:%M:%S')");

builder.Entity<Event>()
.Property(e => e.DateCreated)
.HasDefaultValueSql("strftime('%Y-%m-%d %H:%M:%S')");

builder.Entity<EventChatroomMessage>()
.Property(e => e.DateCreated)
.HasDefaultValueSql("strftime('%Y-%m-%d %H:%M:%S')");

builder.Entity<EventMember>()
.Property(e => e.DateCreated)
.HasDefaultValueSql("strftime('%Y-%m-%d %H:%M:%S')");

builder.Entity<Organization>()
.Property(o => o.DateCreated)
.HasDefaultValueSql("strftime('%Y-%m-%d %H:%M:%S')");

builder.Entity<VolunteerAchievements>()
.Property(v => v.DateCreated)
.HasDefaultValueSql("strftime('%Y-%m-%d %H:%M:%S')");
}
}
}
6 changes: 3 additions & 3 deletions Models/EntityModels/Achievements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
namespace Community.Models
{
/**
* Class: Achievements
* Class: Achievement
* Purpose: Stores information about an achievement
*/
public class Achievements
public class Achievement
{
[Key]
public int AchievementId { get; set; }
Expand All @@ -25,6 +25,6 @@ public class Achievements
[Required]
[DataType(DataType.Date)]
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime DateEarned { get; set; }
public DateTime DateCreated { get; set; }
}
}
7 changes: 7 additions & 0 deletions Models/EntityModels/ApplicationUser.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;

namespace Community.Models
Expand All @@ -23,5 +25,10 @@ public class ApplicationUser : IdentityUser

[Required]
public int ZipCode { get; set; }

[Required]
[DataType(DataType.Date)]
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime DateCreated { get; set; }
}
}
6 changes: 6 additions & 0 deletions Models/EntityModels/Organization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ public class Organization
[Required]
public bool IsActive { get; set; }

[Required]
public string OrganizerId { get; set; }

[Required]
public ApplicationUser Organizer { get; set; }

[Required]
[DataType(DataType.Date)]
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
Expand Down