From 3acb5bc922bef730c5480b12c2bec993622381b8 Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Sun, 11 Dec 2016 14:45:51 +0100 Subject: [PATCH] estimate to client --- .../ApiControllers/BookQueryApiController.cs | 6 ++- Yavsc/ApiControllers/ContactsApiController.cs | 28 ++-------- Yavsc/Helpers/EstimateHelpers.cs | 16 ++++++ Yavsc/Helpers/EventHelpers.cs | 9 ++-- Yavsc/Interfaces/IBookQueryData.cs | 2 +- Yavsc/Interfaces/Workflow/IEvent.cs | 13 +++-- Yavsc/Models/ApplicationDbContext.cs | 2 +- .../Models/Messaging/BookQueryProviderInfo.cs | 2 + Yavsc/Models/Messaging/ClientProviderInfo.cs | 4 +- Yavsc/Models/Messaging/EstimationEvent.cs | 54 +++++++++++++++++++ Yavsc/Models/Messaging/ProviderClientInfo.cs | 8 +++ Yavsc/Services/IGoogleCloudMessageSender.cs | 11 +++- Yavsc/Services/MessageServices.cs | 12 ++++- 13 files changed, 127 insertions(+), 40 deletions(-) create mode 100644 Yavsc/Helpers/EstimateHelpers.cs create mode 100644 Yavsc/Models/Messaging/EstimationEvent.cs create mode 100644 Yavsc/Models/Messaging/ProviderClientInfo.cs diff --git a/Yavsc/ApiControllers/BookQueryApiController.cs b/Yavsc/ApiControllers/BookQueryApiController.cs index 7c8c76d5..8094f74d 100644 --- a/Yavsc/ApiControllers/BookQueryApiController.cs +++ b/Yavsc/ApiControllers/BookQueryApiController.cs @@ -11,6 +11,7 @@ namespace Yavsc.Controllers { using System; using Yavsc.Model; + using Yavsc.Models.Messaging; using Yavsc.Models; using Yavsc.Models.Booking; @@ -43,7 +44,10 @@ namespace Yavsc.Controllers Include(c => c.Client).Where(c => c.PerformerId == uid && c.Id < maxId && c.EventDate > now). Select(c => new BookQueryProviderInfo { - Client = new ClientProviderInfo { UserName = c.Client.UserName, UserId = c.ClientId }, + Client = new ClientProviderInfo { + UserName = c.Client.UserName, + UserId = c.ClientId, + Avatar = c.Client.Avatar }, Location = c.Location, EventDate = c.EventDate, Id = c.Id, diff --git a/Yavsc/ApiControllers/ContactsApiController.cs b/Yavsc/ApiControllers/ContactsApiController.cs index d031c485..6e217d26 100644 --- a/Yavsc/ApiControllers/ContactsApiController.cs +++ b/Yavsc/ApiControllers/ContactsApiController.cs @@ -1,10 +1,9 @@ -using System.Collections.Generic; using System.Linq; using Microsoft.AspNet.Http; using Microsoft.AspNet.Mvc; using Microsoft.Data.Entity; -using Yavsc.Model; using Yavsc.Models; +using Yavsc.Models.Messaging; namespace Yavsc.Controllers { @@ -20,29 +19,10 @@ namespace Yavsc.Controllers } // GET: api/ContactsApi - [HttpGet] - public IEnumerable GetClientProviderInfo() + [HttpGet("{id}")] + public ClientProviderInfo GetClientProviderInfo(string id) { - return _context.ClientProviderInfo; - } - - // GET: api/ContactsApi/5 - [HttpGet("{id}", Name = "GetClientProviderInfo")] - public IActionResult GetClientProviderInfo([FromRoute] string id) - { - if (!ModelState.IsValid) - { - return HttpBadRequest(ModelState); - } - - ClientProviderInfo clientProviderInfo = _context.ClientProviderInfo.Single(m => m.UserId == id); - - if (clientProviderInfo == null) - { - return HttpNotFound(); - } - - return Ok(clientProviderInfo); + return _context.ClientProviderInfo.FirstOrDefault(c=>c.UserId == id); } // PUT: api/ContactsApi/5 diff --git a/Yavsc/Helpers/EstimateHelpers.cs b/Yavsc/Helpers/EstimateHelpers.cs new file mode 100644 index 00000000..5976cff4 --- /dev/null +++ b/Yavsc/Helpers/EstimateHelpers.cs @@ -0,0 +1,16 @@ + +using Yavsc.Models.Billing; + +namespace Yavsc.Helpers +{ + public static class EstimateHelpers { + public static decimal GetTotal(this Estimate estimate) + { + decimal result = 0; + foreach (var l in estimate.Bill) + result += l.Count*l.UnitaryCost; + return result; + } + } + +} \ No newline at end of file diff --git a/Yavsc/Helpers/EventHelpers.cs b/Yavsc/Helpers/EventHelpers.cs index 610b1e47..1461c495 100644 --- a/Yavsc/Helpers/EventHelpers.cs +++ b/Yavsc/Helpers/EventHelpers.cs @@ -1,5 +1,4 @@ using Microsoft.Extensions.Localization; -using Yavsc.Model; using Yavsc.Models.Booking; using Yavsc.Models.Messaging; @@ -12,11 +11,15 @@ namespace Yavsc.Helpers { var yaev = new BookQueryEvent { - Client = new ClientProviderInfo { UserName = query.Client.UserName , UserId = query.ClientId } , + Client = new ClientProviderInfo {  + UserName = query.Client.UserName , + UserId = query.ClientId, + Avatar = query.Client.Avatar } , Previsional = query.Previsional, EventDate = query.EventDate, Location = query.Location, - Id = query.Id + Id = query.Id, + Reason = query.Reason }; return yaev; } diff --git a/Yavsc/Interfaces/IBookQueryData.cs b/Yavsc/Interfaces/IBookQueryData.cs index ab156929..49f8aa03 100644 --- a/Yavsc/Interfaces/IBookQueryData.cs +++ b/Yavsc/Interfaces/IBookQueryData.cs @@ -1,8 +1,8 @@ using System; -using Yavsc.Model; namespace Yavsc.Interfaces { + using Yavsc.Models.Messaging; public interface IBookQueryData { ClientProviderInfo Client { get; set; } diff --git a/Yavsc/Interfaces/Workflow/IEvent.cs b/Yavsc/Interfaces/Workflow/IEvent.cs index 6ee109e7..df7a8714 100644 --- a/Yavsc/Interfaces/Workflow/IEvent.cs +++ b/Yavsc/Interfaces/Workflow/IEvent.cs @@ -2,13 +2,18 @@ public interface IEvent { /// - /// An acceptable topic for this event to be. - /// Should return something like the class name - /// of this object + /// /topic/(bookquery|estimate) /// /// string Topic { get; set ; } + /// + /// Should be the user's name + /// + /// string Sender { get; set ; } - + /// + /// The message + /// + /// string Message { get; set; } } \ No newline at end of file diff --git a/Yavsc/Models/ApplicationDbContext.cs b/Yavsc/Models/ApplicationDbContext.cs index 63f81615..e02c08a0 100644 --- a/Yavsc/Models/ApplicationDbContext.cs +++ b/Yavsc/Models/ApplicationDbContext.cs @@ -12,8 +12,8 @@ using Yavsc.Models.OAuth; using Yavsc.Models.Workflow; using Yavsc.Models.Identity; using Yavsc.Models.Market; -using Yavsc.Model; using Yavsc.Model.Chat; +using Yavsc.Models.Messaging; namespace Yavsc.Models { diff --git a/Yavsc/Models/Messaging/BookQueryProviderInfo.cs b/Yavsc/Models/Messaging/BookQueryProviderInfo.cs index 3e60d194..b34cf5f6 100644 --- a/Yavsc/Models/Messaging/BookQueryProviderInfo.cs +++ b/Yavsc/Models/Messaging/BookQueryProviderInfo.cs @@ -2,6 +2,7 @@ using System; namespace Yavsc.Model { + using Models.Messaging; public class BookQueryProviderInfo { @@ -15,4 +16,5 @@ namespace Yavsc.Model public string Reason { get; set; } } + } diff --git a/Yavsc/Models/Messaging/ClientProviderInfo.cs b/Yavsc/Models/Messaging/ClientProviderInfo.cs index cec01e02..3f9aebec 100644 --- a/Yavsc/Models/Messaging/ClientProviderInfo.cs +++ b/Yavsc/Models/Messaging/ClientProviderInfo.cs @@ -1,6 +1,6 @@ using System.ComponentModel.DataAnnotations; -namespace Yavsc.Model +namespace Yavsc.Models.Messaging { public class ClientProviderInfo { @@ -8,10 +8,8 @@ namespace Yavsc.Model public string Avatar { get; set; } [Key] public string UserId { get; set; } - public int Rate { get; set; } public string EMail { get; set; } public string Phone { get; set; } public Location BillingAddress { get; set; } - public string ChatHubConnectionId { get; set; } } } diff --git a/Yavsc/Models/Messaging/EstimationEvent.cs b/Yavsc/Models/Messaging/EstimationEvent.cs new file mode 100644 index 00000000..31c24c15 --- /dev/null +++ b/Yavsc/Models/Messaging/EstimationEvent.cs @@ -0,0 +1,54 @@ +using System.Linq; +using Microsoft.Extensions.Localization; +using Yavsc.Helpers; +using Yavsc.Models.Billing; + +namespace Yavsc.Models.Messaging +{ + public class EstimationEvent: IEvent + { + public EstimationEvent(ApplicationDbContext context, Estimate estimate, IStringLocalizer SR) + { + Estimation = estimate; + var perfer = context.Performers.FirstOrDefault( + p => p.PerformerId == estimate.OwnerId + ); + // Use estimate.OwnerId; + ProviderInfo = new ProviderClientInfo { + Rate = perfer.Rate, + UserName = perfer.Performer.UserName, + Avatar = perfer.Performer.Avatar, + UserId = perfer.PerformerId + }; + ((IEvent)this).Sender = perfer.Performer.UserName; + ((IEvent)this).Message = string.Format(SR["EstimationMessageToClient"],perfer.Performer.UserName, + estimate.Title,estimate.GetTotal()); + } + ProviderClientInfo ProviderInfo { get; set; } + Estimate Estimation { get; set; } + + private string subtopic = null; + string IEvent.Topic + { + get + { + return "/topic/estimate"+subtopic!=null?"/"+subtopic:""; + } + + set + { + subtopic = value; + } + } + + string IEvent.Sender + { + get; set; + } + + string IEvent.Message + { + get; set; + } + } +} \ No newline at end of file diff --git a/Yavsc/Models/Messaging/ProviderClientInfo.cs b/Yavsc/Models/Messaging/ProviderClientInfo.cs new file mode 100644 index 00000000..6e4ae6db --- /dev/null +++ b/Yavsc/Models/Messaging/ProviderClientInfo.cs @@ -0,0 +1,8 @@ + +namespace Yavsc.Models.Messaging +{ + public class ProviderClientInfo : ClientProviderInfo + { + public int Rate { get; set; } + } +} \ No newline at end of file diff --git a/Yavsc/Services/IGoogleCloudMessageSender.cs b/Yavsc/Services/IGoogleCloudMessageSender.cs index 033759c7..662be685 100644 --- a/Yavsc/Services/IGoogleCloudMessageSender.cs +++ b/Yavsc/Services/IGoogleCloudMessageSender.cs @@ -8,6 +8,15 @@ namespace Yavsc.Services { public interface IGoogleCloudMessageSender { - Task NotifyBookQueryAsync(GoogleAuthSettings googlesettings, IEnumerable registrationId, BookQueryEvent ev); + Task NotifyBookQueryAsync( + GoogleAuthSettings googlesettings, + IEnumerable registrationId, + BookQueryEvent ev); + + Task NotifyEstimateAsync( + GoogleAuthSettings googlesettings, + IEnumerable registrationId, + EstimationEvent ev); + } } diff --git a/Yavsc/Services/MessageServices.cs b/Yavsc/Services/MessageServices.cs index 33f54fd1..d0a0b44c 100755 --- a/Yavsc/Services/MessageServices.cs +++ b/Yavsc/Services/MessageServices.cs @@ -27,8 +27,7 @@ namespace Yavsc.Services /// a MessageWithPayloadResponse, /// bool somethingsent = (response.failure == 0 && response.success > 0) /// - public async Task - NotifyBookQueryAsync(GoogleAuthSettings googleSettings, IEnumerable registrationIds, BookQueryEvent ev) + public async Task NotifyBookQueryAsync(GoogleAuthSettings googleSettings, IEnumerable registrationIds, BookQueryEvent ev) { MessageWithPayloadResponse response = null; await Task.Run(()=>{ @@ -37,6 +36,15 @@ namespace Yavsc.Services return response; } + public async Task NotifyEstimateAsync(GoogleAuthSettings googleSettings, IEnumerable registrationIds, EstimationEvent ev) + { + MessageWithPayloadResponse response = null; + await Task.Run(()=>{ + response = googleSettings.NotifyEvent(registrationIds, ev); + }); + return response; + } + public Task SendEmailAsync(SiteSettings siteSettings, SmtpSettings smtpSettings, string email, string subject, string message) { try