yavsc/Yavsc/Controllers/CommandController.cs

252 lines
9.2 KiB
C#
Raw Normal View History

2016-05-17 18:22:18 +02:00
using System;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Authorization;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Mvc;
using Microsoft.Data.Entity;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.OptionsModel;
using Yavsc.Helpers;
using Yavsc.Models;
using Yavsc.Models.Booking;
using Yavsc.Models.Google.Messaging;
using Yavsc.Services;
namespace Yavsc.Controllers
{
[ServiceFilter(typeof(LanguageActionFilter))]
public class CommandController : Controller
{
private UserManager<ApplicationUser> _userManager;
private ApplicationDbContext _context;
private GoogleAuthSettings _googleSettings;
private IGoogleCloudMessageSender _GCMSender;
private IEmailSender _emailSender;
private IStringLocalizer _localizer;
SiteSettings _siteSettings;
SmtpSettings _smtpSettings;
private readonly ILogger _logger;
2016-06-14 03:37:57 +02:00
public CommandController(ApplicationDbContext context, IOptions<GoogleAuthSettings> googleSettings,
2016-05-17 18:22:18 +02:00
IGoogleCloudMessageSender GCMSender,
UserManager<ApplicationUser> userManager,
2016-05-20 12:44:11 +02:00
IStringLocalizer<Yavsc.Resources.YavscLocalisation> localizer,
2016-05-17 18:22:18 +02:00
IEmailSender emailSender,
IOptions<SmtpSettings> smtpSettings,
IOptions<SiteSettings> siteSettings,
ILoggerFactory loggerFactory)
{
_context = context;
_GCMSender = GCMSender;
_emailSender = emailSender;
_googleSettings = googleSettings.Value;
_userManager = userManager;
_smtpSettings = smtpSettings.Value;
_siteSettings = siteSettings.Value;
_localizer = localizer;
_logger = loggerFactory.CreateLogger<CommandController>();
}
// GET: Command
[Authorize]
2016-05-17 18:22:18 +02:00
public IActionResult Index()
{
var uid = User.GetUserId();
2016-05-17 18:22:18 +02:00
return View(_context.BookQueries
2016-06-14 03:37:57 +02:00
.Include(x => x.Client)
.Include(x => x.PerformerProfile)
.Include(x => x.PerformerProfile.Performer)
.Include(x => x.Location)
.Where(x=> x.ClientId == uid || x.PerformerId == uid)
.ToList());
2016-05-17 18:22:18 +02:00
}
// GET: Command/Details/5
public IActionResult Details(long? id)
{
if (id == null)
{
return HttpNotFound();
}
BookQuery command = _context.BookQueries
2016-06-14 03:37:57 +02:00
.Include(x => x.Location)
.Include(x => x.PerformerProfile)
2016-05-17 18:22:18 +02:00
.Single(m => m.Id == id);
if (command == null)
{
return HttpNotFound();
}
return View(command);
}
[Authorize]
/// <summary>
/// Gives a view on
/// Creating a command for a specified performer
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public IActionResult Create(string id, string activityCode)
2016-05-17 18:22:18 +02:00
{
2016-06-14 03:37:57 +02:00
if (string.IsNullOrWhiteSpace(id))
throw new InvalidOperationException(
"This method needs a performer id"
);
if (string.IsNullOrWhiteSpace(activityCode))
throw new InvalidOperationException(
"This method needs an activity code"
);
2016-05-17 18:22:18 +02:00
var pro = _context.Performers.Include(
2016-06-14 03:37:57 +02:00
x => x.Performer).FirstOrDefault(
2016-07-27 11:08:06 +02:00
x => x.PerformerId == id
2016-05-17 18:22:18 +02:00
);
2016-06-14 03:37:57 +02:00
if (pro == null)
2016-05-17 18:22:18 +02:00
return HttpNotFound();
ViewBag.Activity = _context.Activities.FirstOrDefault(a=>a.Code == activityCode);
2016-05-17 18:22:18 +02:00
ViewBag.GoogleSettings = _googleSettings;
var userid = User.GetUserId();
2016-06-14 03:37:57 +02:00
var user = _userManager.FindByIdAsync(userid).Result;
2017-01-16 01:20:39 +01:00
return View(new BookQuery(activityCode,new Location(),DateTime.Now.AddHours(4))
2016-06-14 03:37:57 +02:00
{
2016-05-17 18:22:18 +02:00
PerformerProfile = pro,
2016-07-27 11:08:06 +02:00
PerformerId = pro.PerformerId,
2016-05-17 18:22:18 +02:00
ClientId = userid,
2016-06-14 03:37:57 +02:00
Client = user
2016-05-17 18:22:18 +02:00
});
}
// POST: Command/Create
2016-06-14 03:37:57 +02:00
[HttpPost, Authorize]
2016-05-17 18:22:18 +02:00
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(BookQuery command)
{
2017-01-16 01:20:39 +01:00
2016-06-14 03:37:57 +02:00
var uid = User.GetUserId();
var prid = command.PerformerId;
if (string.IsNullOrWhiteSpace(uid)
|| string.IsNullOrWhiteSpace(prid))
throw new InvalidOperationException(
"This method needs a prid and uid"
);
var pro = _context.Performers.Include(
u => u.Performer
).Include( u => u.Performer.Devices)
.FirstOrDefault(
2016-07-27 11:08:06 +02:00
x => x.PerformerId == command.PerformerId
2016-05-17 18:22:18 +02:00
);
2016-09-17 03:36:34 +02:00
_logger.LogDebug($"Pro: {pro}");
2016-05-17 18:22:18 +02:00
command.PerformerProfile = pro;
2016-06-14 03:37:57 +02:00
var user = await _userManager.FindByIdAsync(
User.GetUserId()
);
2016-05-17 18:22:18 +02:00
command.Client = user;
if (ModelState.IsValid)
{
var existingLocation = _context.Locations.FirstOrDefault( x=>x.Address == command.Location.Address
&& x.Longitude == command.Location.Longitude && x.Latitude == command.Location.Latitude );
2016-05-17 18:22:18 +02:00
if (existingLocation!=null) {
command.Location=existingLocation;
}
else _context.Attach<Location>(command.Location);
2016-06-14 03:37:57 +02:00
_context.BookQueries.Add(command, GraphBehavior.IncludeDependents);
2016-05-17 18:22:18 +02:00
_context.SaveChanges();
2016-06-14 03:37:57 +02:00
var yaev = command.CreateEvent(_localizer);
MessageWithPayloadResponse grep = null;
2016-06-14 03:37:57 +02:00
if (pro.AcceptNotifications
&& pro.AcceptPublicContact)
2016-05-17 18:22:18 +02:00
{
if (pro.Performer.Devices.Count > 0) {
var regids = command.PerformerProfile.Performer
.Devices.Select(d => d.GCMRegistrationId);
2016-09-17 03:36:34 +02:00
grep = await _GCMSender.NotifyBookQueryAsync(_googleSettings,regids,yaev);
}
2016-06-14 03:37:57 +02:00
// TODO setup a profile choice to allow notifications
// both on mailbox and mobile
// if (grep==null || grep.success<=0 || grep.failure>0)
ViewBag.GooglePayload=grep;
if (grep!=null)
_logger.LogWarning($"Performer: {command.PerformerProfile.Performer.UserName} success: {grep.success} failure: {grep.failure}");
2016-06-14 03:37:57 +02:00
2016-05-17 18:22:18 +02:00
await _emailSender.SendEmailAsync(
_siteSettings, _smtpSettings,
command.PerformerProfile.Performer.Email,
2016-09-17 03:36:34 +02:00
yaev.Topic+" "+yaev.Client.UserName,
$"{yaev.Message}\r\n-- \r\n{yaev.Previsional}\r\n{yaev.EventDate}\r\n"
2016-05-17 18:22:18 +02:00
);
}
2017-01-16 01:20:39 +01:00
ViewBag.Activity = _context.Activities.FirstOrDefault(a=>a.Code == command.ActivityCode);
ViewBag.GoogleSettings = _googleSettings;
return View("CommandConfirmation",command);
2016-05-17 18:22:18 +02:00
}
ViewBag.GoogleSettings = _googleSettings;
return View(command);
}
// GET: Command/Edit/5
public IActionResult Edit(long? id)
{
if (id == null)
{
return HttpNotFound();
}
BookQuery command = _context.BookQueries.Single(m => m.Id == id);
if (command == null)
{
return HttpNotFound();
}
return View(command);
}
// POST: Command/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Edit(BookQuery command)
{
if (ModelState.IsValid)
{
_context.Update(command);
_context.SaveChanges();
return RedirectToAction("Index");
}
return View(command);
}
// GET: Command/Delete/5
[ActionName("Delete")]
public IActionResult Delete(long? id)
{
if (id == null)
{
return HttpNotFound();
}
BookQuery command = _context.BookQueries.Single(m => m.Id == id);
if (command == null)
{
return HttpNotFound();
}
return View(command);
}
// POST: Command/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public IActionResult DeleteConfirmed(long id)
{
BookQuery command = _context.BookQueries.Single(m => m.Id == id);
_context.BookQueries.Remove(command);
_context.SaveChanges();
return RedirectToAction("Index");
}
}
}