From f22f722f364fc86d7e88765df4ae796a4a1170d2 Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Sat, 8 Jul 2017 20:27:36 +0200 Subject: [PATCH] input filters --- .../Haircut/HairCutCommandController.cs | 47 ++++++++++--------- Yavsc/CustumModelBinder.cs | 31 ++++++++++-- Yavsc/Services/GoogleApis/CalendarManager.cs | 2 - Yavsc/Startup/Startup.cs | 5 ++ Yavsc/Views/HairCutCommand/HairCut.cshtml | 8 ++-- 5 files changed, 63 insertions(+), 30 deletions(-) diff --git a/Yavsc/Controllers/Haircut/HairCutCommandController.cs b/Yavsc/Controllers/Haircut/HairCutCommandController.cs index 9051852a..0c12da0b 100644 --- a/Yavsc/Controllers/Haircut/HairCutCommandController.cs +++ b/Yavsc/Controllers/Haircut/HairCutCommandController.cs @@ -254,30 +254,36 @@ Le client final: {clientFinal} if (ModelState.IsValid) { - // Une prestation pour enfant ou homme inclut toujours la coupe. - if (model.Prestation.Gender != HairCutGenders.Women) - model.Prestation.Cut = true; - if (model.Location!=null) { - var existingLocation = await _context.Locations.FirstOrDefaultAsync( x=>x.Address == model.Location.Address - && x.Longitude == model.Location.Longitude && x.Latitude == model.Location.Latitude ); - - if (existingLocation!=null) { - model.Location=existingLocation; + + using (var trans = _context.Database.BeginTransaction()) { + // Une prestation pour enfant ou homme inclut toujours la coupe. + if (model.Prestation.Gender != HairCutGenders.Women) + model.Prestation.Cut = true; + if (model.Location!=null) { + var existingLocation = await _context.Locations.FirstOrDefaultAsync( x=>x.Address == model.Location.Address + && x.Longitude == model.Location.Longitude && x.Latitude == model.Location.Latitude ); + + if (existingLocation!=null) { + model.Location=existingLocation; + } + else _context.Attach(model.Location); } - else _context.Attach(model.Location); - } - var existingPrestation = await _context.HairPrestation.FirstOrDefaultAsync( x=> model.PrestationId == x.Id ); + var existingPrestation = await _context.HairPrestation.FirstOrDefaultAsync( x=> model.PrestationId == x.Id ); - if (existingPrestation!=null) { - model.Prestation = existingPrestation; + if (existingPrestation!=null) { + model.Prestation = existingPrestation; + } + else _context.Attach(model.Prestation); + + _context.HairCutQueries.Add(model); + var brusherProfile = await _context.BrusherProfile.SingleAsync(p=>p.UserId == pro.PerformerId); + model.Client = await _context.Users.SingleAsync(u=>u.Id == model.ClientId); + model.SelectedProfile = brusherProfile; + + await _context.SaveChangesAsync(uid); + trans.Commit(); } - else _context.Attach(model.Prestation); - _context.HairCutQueries.Add(model); - var brusherProfile = await _context.BrusherProfile.SingleAsync(p=>p.UserId == pro.PerformerId); - model.Client = await _context.Users.SingleAsync(u=>u.Id == model.ClientId); - model.SelectedProfile = brusherProfile; - await _context.SaveChangesAsync(uid); var yaev = model.CreateNewHairCutQueryEvent(_localizer); MessageWithPayloadResponse grep = null; @@ -326,7 +332,6 @@ Le client final: {clientFinal} var items = model.GetBillItems(); var addition = items.Addition(); ViewBag.Addition = addition.ToString("C",CultureInfo.CurrentUICulture); - return View("CommandConfirmation",model); } ViewBag.Activity = _context.Activities.FirstOrDefault(a=>a.Code == model.ActivityCode); diff --git a/Yavsc/CustumModelBinder.cs b/Yavsc/CustumModelBinder.cs index 5ba4415f..59e3be98 100644 --- a/Yavsc/CustumModelBinder.cs +++ b/Yavsc/CustumModelBinder.cs @@ -1,6 +1,8 @@ using System; +using System.Globalization; using System.Threading.Tasks; using Microsoft.AspNet.Mvc.ModelBinding; +using Newtonsoft.Json; namespace Yavsc { @@ -9,13 +11,12 @@ namespace Yavsc public async Task BindModelAsync(ModelBindingContext bindingContext) { - ValueProviderResult valueResult = bindingContext.ValueProvider + Console.WriteLine(JsonConvert.SerializeObject(bindingContext)); + ValueProviderResult valueResult = bindingContext.ValueProvider .GetValue(bindingContext.ModelName); decimal actualValue ; - ModelStateEntry modelState = new ModelStateEntry(); try { actualValue = Decimal.Parse(valueResult.FirstValue, System.Globalization.NumberStyles.AllowDecimalPoint); - return await ModelBindingResult.SuccessAsync(bindingContext.ModelName,actualValue); } catch (Exception ) { @@ -23,4 +24,28 @@ namespace Yavsc return await ModelBindingResult.FailedAsync(bindingContext.ModelName); } } + + public class MyDateTimeModelBinder : IModelBinder + { + public async Task BindModelAsync(ModelBindingContext bindingContext) + { + Console.WriteLine(JsonConvert.SerializeObject(bindingContext.ValueProvider)); + ValueProviderResult valueResult = bindingContext.ValueProvider + .GetValue(bindingContext.ModelName); + Console.WriteLine(JsonConvert.SerializeObject(valueResult)); + DateTime actualValue ; + ModelStateEntry modelState = new ModelStateEntry(); + CultureInfo[] cultures = { new CultureInfo("en-US"), + new CultureInfo("fr-FR"), + new CultureInfo("it-IT"), + new CultureInfo("de-DE") }; + foreach (CultureInfo culture in cultures) + if (DateTime.TryParse(valueResult.FirstValue,culture, DateTimeStyles.AllowInnerWhite, out actualValue)) + { + return await ModelBindingResult.SuccessAsync(bindingContext.ModelName,actualValue); + } + + return await ModelBindingResult.FailedAsync(bindingContext.ModelName); + } + } } \ No newline at end of file diff --git a/Yavsc/Services/GoogleApis/CalendarManager.cs b/Yavsc/Services/GoogleApis/CalendarManager.cs index d536b94a..ee57708d 100644 --- a/Yavsc/Services/GoogleApis/CalendarManager.cs +++ b/Yavsc/Services/GoogleApis/CalendarManager.cs @@ -36,7 +36,6 @@ namespace Yavsc.Services { using System.Threading; using Google.Apis.Auth.OAuth2.Flows; - using Newtonsoft.Json; using Yavsc.Helpers; using Yavsc.Models; using Yavsc.Models.Calendar; @@ -79,7 +78,6 @@ namespace Yavsc.Services Scopes = new[] { scopeCalendar }, DataStore = dataStore }); - _logger.LogWarning($"Using Google data store from "+JsonConvert.SerializeObject(_dataStore)); } /// diff --git a/Yavsc/Startup/Startup.cs b/Yavsc/Startup/Startup.cs index 211fca10..a58e44e3 100755 --- a/Yavsc/Startup/Startup.cs +++ b/Yavsc/Startup/Startup.cs @@ -205,6 +205,8 @@ namespace Yavsc .Build(); config.Filters.Add(new AuthorizeFilter(policy)); config.Filters.Add(new ProducesAttribute("application/json")); + config.ModelBinders.Add(new MyDateTimeModelBinder()); + config.ModelBinders.Add(new MyDecimalModelBinder()); config.OutputFormatters.Add(new PdfFormatter()); }).AddFormatterMappings( @@ -245,6 +247,7 @@ namespace Yavsc public static IStringLocalizer GlobalLocalizer { get; private set; } + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, IOptions siteSettings, @@ -338,6 +341,7 @@ namespace Yavsc else throw ex; } } + // before fixing the security protocol, let beleive our lib it's done with it. var cxmgr = ConnectionManager.Instance; // then, fix it. @@ -361,6 +365,7 @@ namespace Yavsc routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); + }); logger.LogInformation("LocalApplicationData: "+Environment.GetFolderPath(SpecialFolder.LocalApplicationData, SpecialFolderOption.DoNotVerify)); diff --git a/Yavsc/Views/HairCutCommand/HairCut.cshtml b/Yavsc/Views/HairCutCommand/HairCut.cshtml index 72536cc6..99da083f 100644 --- a/Yavsc/Views/HairCutCommand/HairCut.cshtml +++ b/Yavsc/Views/HairCutCommand/HairCut.cshtml @@ -181,8 +181,8 @@ var pos = loc.geometry.location; var lat = new Number(pos.lat); var lng = new Number(pos.lng); - $('#' + config.latId).val(lat); - $('#' + config.longId).val(lng); + $('#' + config.latId).val(lat.toLocaleString('en')); + $('#' + config.longId).val(lng.toLocaleString('en')); gmap.setCenter(pos); if (marker) { marker.setMap(null); @@ -196,8 +196,8 @@ google.maps.event.addListener(marker, 'dragend', function () { // TODO reverse geo code var pos = marker.getPosition(); - $('#' + config.latId).val(pos.lat); - $('#' + config.longId).val(pos.lng); + $('#' + config.latId).val(pos.lat.toLocaleString('en')); + $('#' + config.longId).val(pos.lng.toLocaleString('en')); }); $('#' + config.addrId).valid(); $('#' + config.addrValidationId).empty();