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 Microsoft.AspNetCore.Authorization; using rockfishCore.Models; using rockfishCore.Util; namespace rockfishCore.Controllers { [Produces("application/json")] [Route("api/Customer")] [Authorize] public class CustomerController : Controller { private readonly rockfishContext ct; public CustomerController(rockfishContext context) { ct = context; } //Get api/customer/list [HttpGet("list")] public IEnumerable GetList() { var res = from c in ct.Customer.OrderBy(c => c.Name) select new dtoCustomerListItem { lapsed = false, active = c.Active, id = c.Id, name = c.Name }; //Need to be made into a list before updating or else the changes would be lost //The tolist is what triggers the actual query to run var LapsedRes = res.ToList(); foreach (dtoCustomerListItem i in LapsedRes) { i.lapsed = CustomerHasLapsed(i.id); } return LapsedRes; } // //Get api/customer/list // [HttpGet("list")] // public ActionResult GetList() // { // var res = from c in _context.Customer.OrderBy(c => c.Name) // select new dtoCustomerListItem // { // lapsed = false, // active = c.Active, // id = c.Id, // name = c.Name // }; // //Need to be made into a list before updating or else the changes would be lost // //The tolist is what triggers the actual query to run // var LapsedRes = res.ToList(); // foreach (dtoCustomerListItem i in LapsedRes) // { // i.lapsed = CustomerHasLapsed(i.id); // } // return Ok(LapsedRes); // } /// /// Check if a customer has any active subs /// return true if no active subs /// Active means cancel date is empty or not yet come to pass /// and expire date is not empty and not yet come to pass /// /// /// private bool CustomerHasLapsed(long customerId) { long EpochNow = DateUtil.NowAsEpoch(); var count = ct.Purchase .Where(c => c.CustomerId.Equals(customerId)) .Where(c => c.CancelDate == null || c.CancelDate > EpochNow) .Where(c => c.ExpireDate != null && c.ExpireDate > EpochNow) .Count(); //handle raven keys here // var ravActiveMaintenanceCount=ct.License.Where(z=>z.CustomerId==customerId && z.) return count < 1; } //Get api/customer/77/sitelist [HttpGet("{id}/sitelist")] public IEnumerable GetSiteList([FromRoute] long id) { var res = from c in ct.Site .Where(c => c.CustomerId.Equals(id)).OrderBy(c => c.Name) select new dtoNameIdItem { id = c.Id, name = c.Name }; return res.ToList(); } //Get api/customer/77/activesubbysite [HttpGet("{id}/activesubforsites")] public IEnumerable GetActiveSubsForSites([FromRoute] long id) { long EpochNow = DateUtil.NowAsEpoch(); var res = from c in ct.Site .Where(c => c.CustomerId.Equals(id)).OrderBy(c => c.Name) select new dtoNameIdChildrenItem { id = c.Id, name = c.Name }; //Force immediate query execution var resList = res.ToList(); foreach (dtoNameIdChildrenItem child in resList) { var subs = from c in ct.Purchase .Where(c => c.SiteId.Equals(child.id)) .Where(c => c.CancelDate == null || c.CancelDate > EpochNow) .OrderByDescending(c => c.PurchaseDate) select new dtoNameIdItem { id = c.Id, name = c.Name + " exp: " + DateUtil.EpochToString(c.ExpireDate, "d") }; foreach (dtoNameIdItem sub in subs) { child.children.Add(sub); } } return resList; } //Get api/customer/77/sites [HttpGet("{id}/sites")] public IEnumerable GetSites([FromRoute] long id) { //from https://docs.microsoft.com/en-us/ef/core/querying/basic var sites = ct.Site .Where(b => b.CustomerId.Equals(id)) .OrderByDescending(b => b.Id) .ToList(); return sites; } // //Get api/customer/77/contacts // [HttpGet("{id}/contacts")] // public IEnumerable GetContacts([FromRoute] long id) // { // var contacts = _context.Contact // .Where(b => b.CustomerId.Equals(id)) // .OrderByDescending(b => b.Id) // .ToList(); // return contacts; // } // //Get api/customer/77/notifications // [HttpGet("{id}/notifications")] // public IEnumerable GetNotifications([FromRoute] long id) // { // var notifications = _context.Notification // .Where(b => b.CustomerId.Equals(id)) // .OrderByDescending(b => b.SentDate) // .ToList(); // return notifications; // } //Get api/customer/77/name [HttpGet("{id}/name")] public async Task GetName([FromRoute] long id) { if (!ModelState.IsValid) { return BadRequest(ModelState); } //BUGBUG: this is bombing once in a while //System.InvalidOperationException: Sequence contains no elements //on this client url //http://localhost:5000/default.htm#!/customerSites/85 var ret = await ct.Customer .Select(r => new { r.Id, r.Name }) .Where(r => r.Id == id) .FirstAsync(); return Ok(ret); } //------------- //CRUD ROUTES //------------- // GET: api/Customer //????? DA FUCK IS THIS ROUTE FOR ????? [HttpGet] public IEnumerable GetCustomer() { return ct.Customer; } // GET: api/Customer/5 [HttpGet("{id}")] public async Task GetCustomer([FromRoute] long id) { if (!ModelState.IsValid) { return BadRequest(ModelState); } var customer = await ct.Customer.SingleOrDefaultAsync(m => m.Id == id); if (customer == null) { return NotFound(); } return Ok(customer); } // PUT: api/Customer/5 [HttpPut("{id}")] public async Task PutCustomer([FromRoute] long id, [FromBody] Customer customer) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != customer.Id) { return BadRequest(); } ct.Entry(customer).State = EntityState.Modified; try { await ct.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CustomerExists(id)) { return NotFound(); } else { throw; } } return NoContent(); } // POST: api/Customer [HttpPost] public async Task PostCustomer([FromBody] Customer customer) { if (!ModelState.IsValid) { return BadRequest(ModelState); } ct.Customer.Add(customer); await ct.SaveChangesAsync(); return CreatedAtAction("GetCustomer", new { id = customer.Id }, customer); } // DELETE: api/Customer/5 [HttpDelete("{id}")] public async Task DeleteCustomer([FromRoute] long id) { if (!ModelState.IsValid) { return BadRequest(ModelState); } var customer = await ct.Customer.SingleOrDefaultAsync(m => m.Id == id); if (customer == null) { return NotFound(); } ct.Customer.Remove(customer); await ct.SaveChangesAsync(); return Ok(customer); } private bool CustomerExists(long id) { return ct.Customer.Any(e => e.Id == id); } } }