From 4f96ae19a2d5c9adb9dcc6e54421f25556a7932f Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Wed, 27 Jul 2016 10:58:54 +0200 Subject: [PATCH] =?UTF-8?q?GCM=20register:=20renvoie=20un=20bool=C3=A9en?= =?UTF-8?q?=20faux=20=C3=A0=20une=20premi=C3=A8re=20d=C3=A9claration?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Yavsc/ApiController/GCMController.cs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Yavsc/ApiController/GCMController.cs b/Yavsc/ApiController/GCMController.cs index 10f817b9..1eb24391 100644 --- a/Yavsc/ApiController/GCMController.cs +++ b/Yavsc/ApiController/GCMController.cs @@ -1,3 +1,5 @@ + +using System; using System.Linq; using System.Security.Claims; using Microsoft.AspNet.Authorization; @@ -29,13 +31,16 @@ public class GCMController : Controller [FromBody] GoogleCloudMobileDeclaration declaration) { var uid = User.GetUserId(); + _logger.LogWarning($"Registering device with id:{declaration.DeviceId} for {uid}"); if (ModelState.IsValid) { - if (_context.GCMDevices.Any(d => d.DeviceId == declaration.DeviceId)) + var alreadyRegisteredDevice = _context.GCMDevices.FirstOrDefault(d => d.DeviceId == declaration.DeviceId); + var deviceAlreadyRegistered = (alreadyRegisteredDevice!=null); + if (deviceAlreadyRegistered) { - var alreadyRegisteredDevice = _context.GCMDevices.FirstOrDefault(d => d.DeviceId == declaration.DeviceId); // Override an exiting owner + alreadyRegisteredDevice.DeclarationDate = DateTime.Now; alreadyRegisteredDevice.DeviceOwnerId = uid; alreadyRegisteredDevice.GCMRegistrationId = declaration.GCMRegistrationId; alreadyRegisteredDevice.Model = declaration.Model; @@ -46,11 +51,12 @@ public class GCMController : Controller } else { + declaration.DeclarationDate = DateTime.Now; declaration.DeviceOwnerId = uid; _context.GCMDevices.Add(declaration); _context.SaveChanges(); } - return Ok(); + return Json(new { IsAnUpdate = deviceAlreadyRegistered }); } return new BadRequestObjectResult(ModelState); }