public ActionResult Edit(NoteTable notetable)
 {
     if (ModelState.IsValid)
     {
         db.Entry(notetable).State = EntityState.Modified;
         db.SaveChanges();
         return RedirectToAction("Index");
     }
     ViewBag.AdresseId = new SelectList(db.AdresseTables, "AdresseId", "AptNo", notetable.AdresseId);
     ViewBag.UserId = new SelectList(db.UserTables, "UserId", "Nom", notetable.UserId);
     return View(notetable);
 }
        public ActionResult Create(NoteTable notetable)
        {
            if (ModelState.IsValid)
            {
                db.NoteTables.Add(notetable);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.AdresseId = new SelectList(db.AdresseTables, "AdresseId", "AptNo", notetable.AdresseId);
            ViewBag.UserId = new SelectList(db.UserTables, "UserId", "Nom", notetable.UserId);
            return View(notetable);
        }
 public virtual void SaveNote(NoteTable note, out string resultMessage)
 {
      using (var ctx = new notetalocEntities())
     {
      var noteToAdd = ctx.NoteTables.Where(n => n.AdresseId == note.AdresseId && n.UserId == note.UserId).FirstOrDefault();
          
      if (noteToAdd == null)
      {
          note.NoteId = GetNoteId(DateTime.Now);
          ctx.NoteTables.Add(note);
          ctx.SaveChanges();
          resultMessage = "Add";
      }
      else
      {
          note.NoteId = noteToAdd.NoteId;
          noteToAdd.Note = note.Note;
          ctx.SaveChanges();
          resultMessage = "Update";
      }
     }
 }
 public virtual void UpdateNoteStatus(NoteTable note)
 {
     using (var ctx = new notetalocEntities())
     {
         var noteToUpdate = ctx.NoteTables.Where(n => n.NoteId == note.NoteId).FirstOrDefault();
         noteToUpdate.StatutNote = 1;
         ctx.SaveChanges();
     }
 }
        public ActionResult Index(SaisiNoteForm form)
        {
            if (ModelState.IsValid)
            {
                var address = ConcatenationAddress(form);
                try
                {
                    var geocoder = new Geocoder();
                    var response = geocoder.Locate(address);
                    if (response != null)
                    {
                        var longititude = response.Longitude;
                        var lat = response.Latitude;
                        var formatted_address = response.Formatted_address;
                        var addressToSave = new AdresseTable();
                        addressToSave.AptNo = form.Appartement;
                        addressToSave.CodePostal = form.CodePostal;
                        addressToSave.Ville = form.Localite;
                        addressToSave.Pays = form.Pays;
                        addressToSave.Province = form.Region;
                        addressToSave.GeoCodeResponse = formatted_address;
                        addressToSave.Lattitude = (decimal)lat;
                        addressToSave.Longitude = (decimal)longititude;

                        var saisiNoteWriter = new SaisiNoteWriter(new MailSender(), new SaisiNoteContext());
                        saisiNoteWriter.SaveAddresNoteSaisi(addressToSave);
                        var id = saisiNoteWriter.GetAddressId(addressToSave);
                        var noteToSave = new NoteTable();
                        noteToSave.Note = form.Note;
                        noteToSave.AdresseId = id;
                        noteToSave.UserId = 1;
                        noteToSave.StatutNote = 0;
                        //saisiNoteWriter.SaveNoteSaisi(noteToSave);
                        ViewBag.Message = "Enregistrement reussie!";
                        ViewBag.NumTimes = 1;
                        ViewData["color"] = "green";
                    }
                    else
                    {
                        ViewBag.Message = "vous devez inserez une adresse valide!";
                        ViewBag.NumTimes = 1;
                        ViewData["color"] = "red";
                    }

                }
                catch (Exception ex)
                {
                    ViewBag.Message = "La note n'a pas été enregistrée. Réessayer plus tard!";
                    ViewBag.NumTimes = 1;
                    ViewData["color"] = "red";
                }
            }
            return View(form);
        }
        //
        // POST: /SaisiNote/NoterAppartement

        public ActionResult NoterAppartement(string address, string country, string zip, string provincia, string citta, string appart, string lng, string lat, string nota)
        {
            //SaisiNoteForm form = new SaisiNoteForm();

            var data = new
            {
                indirizzo = address,
                Pays = country,
                zip = zip,
                province = provincia,
                ville = citta,
                appartement = appart,
                longitude = lng,
                latitude = lat,
                note = nota
            };
            string resultMessage = "";
            var result = new { returnValue = "" };
            try
            {
                var valLng = Double.Parse(lng, CultureInfo.InvariantCulture);
                var valLat = Double.Parse(lat, CultureInfo.InvariantCulture);

                var addressToSave = new AdresseTable();
                addressToSave.RueNo = int.Parse(GetRueAndNumero(address)[0]);
                addressToSave.Rue = GetRueAndNumero(address)[1];
                addressToSave.AptNo = appart;
                addressToSave.CodePostal = zip;
                addressToSave.Ville = citta;
                addressToSave.Pays = country;
                addressToSave.Province = provincia;
                addressToSave.GeoCodeResponse = address;
                addressToSave.Lattitude = (decimal)valLat;
                addressToSave.Longitude = (decimal)valLng;

                var saisiNoteWriter = new SaisiNoteWriter(new MailSender(), new SaisiNoteContext());
                saisiNoteWriter.SaveAddresNoteSaisi(addressToSave);
                var id = saisiNoteWriter.GetAddressId(addressToSave);
                var noteToSave = new NoteTable();
                noteToSave.Note = int.Parse(nota);
                noteToSave.AdresseId = id;

                UserTable userVariable = (UserTable)HttpContext.Session["UserSessionObject"];

                noteToSave.UserId = userVariable.UserId;
                noteToSave.StatutNote = 0;
                saisiNoteWriter.SaveNoteSaisi(noteToSave, out resultMessage);
                result = new { returnValue = resultMessage };
            }
            catch (Exception ex)
            {
                result = new { returnValue = " Error" };
            }

            return Json(result);
            //return RedirectToAction("SearchNoted", "AdresseTable", "searchPhrase=7060 Rue Hutchison, Montréal, QC H3N 1Y6, Canada");
        }
        public void SaveNoteSaisi(NoteTable note, out string resultMessage)
        {
            var config = WebConfigurationManager.OpenWebConfiguration("~");
            string receiverMail = config.AppSettings.Settings["ReceiverMail"].Value;
            string senderMail = config.AppSettings.Settings["SenderMail"].Value;
            var mailTo = receiverMail;
            var mailFrom = senderMail;

            var msg = "Cher Administrateur, \nUn appartement [avec une note] dont la note est égale à zéro vient d'être ajouté.\nLes informations de l’appartement sont disponibles ici : [lien vers une page d’administration de l’appartement].\n\nBonne journée.\n";
            var obj = "NoteTaLoc - Notification : note à 0";

            _SaisiNoteContext.SaveNote(note, out resultMessage);
            if (note.Note == 0)
            {
                _MailSender.SendMail(mailTo, mailFrom, obj, msg);
            }
            else
            {
                _SaisiNoteContext.UpdateNoteStatus(note);
            }
        }