public void AddEntry(GuestbookEntry entry)
        {
            entry.DateAdded = DateTime.Now; // setta il campo mancante

            _db.Entries.Add(entry);         // aggiunge l'oggetto all'EntriesDbSet del GuestbookContext
            _db.SaveChanges();              // scrive sul database la nuova entry
        }
        // POST api/guestbookentry
        public HttpResponseMessage Post(GuestbookEntry value)
        {
            if (!ModelState.IsValid)
            {
                var errors =
                    (from state in ModelState
                     where state.Value.Errors.Any()
                     select new
                {
                    state.Key,
                    Errors = state.Value.Errors.Select(
                        error => error.ErrorMessage)
                })
                    .ToDictionary(error => error.Key, error => error.Errors);
                return(Request.CreateResponse(
                           HttpStatusCode.BadRequest, errors));
            }
            _repository.AddEntry(value);
            var response = Request.CreateResponse(
                HttpStatusCode.Created,
                value, Configuration);

            response.Headers.Location = new Uri(Request.RequestUri,
                                                "/api/guestbookentry/"
                                                + value.Id);
            return(response);
        }
        public static int SeedAsync(GuestbookDbContext context)
        {
            // Use the Migrate method to automatically create the database and migrat if needed
            context.Database.EnsureCreated();

            if (context.Guestbooks.Count() == 0)
            {
                // we could also check for some specific instances and behave accordingly to the result.
                Guestbook guestbook = new Guestbook {
                    Name = "Novia guestbook"
                };
                IGuestbookEntry firstEntry = new GuestbookEntry
                {
                    Message = "Welcome to Novia!"
                };
                guestbook.AddEntry(firstEntry);
                IGuestbookEntry secondEntry = new GuestbookEntry
                {
                    Message = "IT profile salutes you."
                };
                guestbook.AddEntry(secondEntry);

                context.Guestbooks.Add(guestbook);
                return(context.SaveChanges());
            }
            return(0);
        }
Beispiel #4
0
 public ActionResult Create(GuestbookEntry entry)
 {
     entry.DateAdded = DateTime.Now;
     db.Entries.Add(entry);
     db.SaveChanges();
     return(RedirectToAction("Index"));
 }
Beispiel #5
0
        public ActionResult DeleteConfirmed(int id)
        {
            GuestbookEntry guestbookEntry = db.Entries.Find(id);

            db.Entries.Remove(guestbookEntry);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
        public IActionResult AddGuestbookEntry(string guestname, string guestmessage)
        {
            var entry = new GuestbookEntry(guestname, guestmessage);

            Guestbook.ListOfGuestbookEntries.Add(entry);

            return(RedirectToAction("Index"));
        }
Beispiel #7
0
        public ActionResult <IEnumerable <GuestbookEntry> > Post([FromBody] GuestbookEntry value)
        {
            _logger.LogInformation("called");

            value.Date = DateTime.UtcNow;
            _repository.Save(value);
            return(RedirectToAction("Get"));
        }
        public IActionResult NewEntry(int id, [FromBody] GuestbookEntry entry)
        {
            var guestbook = _guestbookRepository.GetById(id);

            guestbook.AddEntry(entry);
            _guestbookRepository.Update(guestbook);

            return(Ok(guestbook));
        }
 public ActionResult Create(GuestbookEntry entry)
 {
     if (ModelState.IsValid)
     {
         _repository.AddEntry(entry);
         return(RedirectToAction("Index")); // redirect all'azione Index()
     }
     return(View(entry));                   // se la validazione dei dati inseriti fallisce torna alla vista di inserimento dove l'utente può correggerli
 }
Beispiel #10
0
 public ActionResult Edit([Bind(Include = "Id,Name,Message,DateCreated")] GuestbookEntry guestbookEntry)
 {
     if (ModelState.IsValid)
     {
         db.Entry(guestbookEntry).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(guestbookEntry));
 }
Beispiel #11
0
        public ActionResult Create([Bind(Include = "Id,Name,Message,DateCreated")] GuestbookEntry guestbookEntry)
        {
            if (ModelState.IsValid)
            {
                db.Entries.Add(guestbookEntry);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(guestbookEntry));
        }
 public ActionResult Create(GuestbookEntry entry)
 {
     if (ModelState.IsValid)
     {
         entry.DateAdded = DateTime.Now;
         _db.Entries.Add(entry);
         _db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(entry));
 }
Beispiel #13
0
 public static GuestbookEntryDTO FromEntry(GuestbookEntry entry)
 {
     return(new GuestbookEntryDTO()
     {
         DateTimeCreated = entry.DateTimeCreated,
         EmailAddress = entry.EmailAddress,
         GuestbookId = entry.GuestbookId,
         Id = entry.Id,
         Message = entry.Message
     });
 }
        public async Task <bool> InsertGuestbookEntry(GuestbookEntry entry)
        {
            const string sql = @"INSERT INTO GuestbookEntry VALUES (@Name, @Message, @Date)
                                SELECT SCOPE_IDENTITY();";

            using (var db = _sqlConnectionFactory.Open())
            {
                var result = await db.QueryAsync <int>(sql, entry);

                return(result.Single() > 0);
            }
        }
Beispiel #15
0
        public ActionResult Create([Bind(Include = "Id,Name,Comment")] GuestbookEntry guestbookEntry)
        {
            guestbookEntry.DatePosted = DateTime.Now;
            if (ModelState.IsValid)
            {
                db.GuestbookEntries.Add(guestbookEntry);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(guestbookEntry));
        }
Beispiel #16
0
        public async Task AddGuestBook(GusetBookViewModel model)
        {
            var ret = new GuestbookEntry()
            {
                Name      = model.Name,
                Mail      = model.Mail,
                DateAdded = DateTime.Now,
                Message   = model.Message
            };

            db.Users.Add(ret);
            await db.SaveChangesAsync();
        }
Beispiel #17
0
        // GET: GuestbookEntries/Delete/5
        public ActionResult Delete(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            GuestbookEntry guestbookEntry = db.Entries.Find(id);

            if (guestbookEntry == null)
            {
                return(HttpNotFound());
            }
            return(View(guestbookEntry));
        }
Beispiel #18
0
        public void RecordEntry(Guestbook guestbook, GuestbookEntry newEntry)
        {
            List <GuestbookEntry> guestbookEntries = _repository.List <GuestbookEntry>();

            foreach (var entry in guestbookEntries)
            {
                _messageSender.SendGuestbookNotificationEmail(entry.EmailAddress, "New guestbook entry added");
            }

            guestbook.Entries.Clear();
            guestbook.Entries.AddRange(guestbookEntries); // maintain existing Guestbook Entries
            guestbook.Entries.Add(newEntry);
            _repository.Update(guestbook);
        }
 public ActionResult CreateMessagePartial(GuestbookEntry entry)
 {
     if (ModelState.IsValid)
     {
         entry.DateAdded = DateTime.Now;
         db.GuestbookEntries.Add(entry);
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     else
     {
         // validation error
         return(View());
     }
 }
Beispiel #20
0
        public void RecordEntry(Guestbook guestbook, GuestbookEntry entry)
        {
            guestbook.AddEntry(entry);
            _guestbookRepository.Update(guestbook);

            // send updates to previous entries made within last day
            var emailsToNotify = guestbook.Entries
                                 .Where(e => e.DateTimeCreated > DateTimeOffset.UtcNow.AddDays(-1))
                                 .Select(e => e.EmailAddress);

            foreach (var emailAddress in emailsToNotify)
            {
                string messageBody = "{entry.EmailAddress} left new message {entry.Message}";
                _messageSender.SendGuestbookNotificationEmail(emailAddress, messageBody);
            }
        }
        public IActionResult NewEntry(int id, [FromBody] GuestbookEntry guestbookEntry)
        {
            var guestbook = _repository.GetById <Guestbook>(id);
            //if (guestbook == null)
            //{
            //    return NotFound(id);
            //}
            var entries = _repository.List <GuestbookEntry>();

            guestbook.Entries.Clear();
            guestbook.Entries.AddRange(entries);
            guestbook.AddEntry(guestbookEntry);
            _repository.Update(guestbook);

            return(Ok(guestbook));
        }
        public ActionResult Edit(GuestbookEntry entry)
        {
            var entryToUpdate = _repository.FindById(entry.Id);

            entryToUpdate.DateAdded = DateTime.Now;
            if (TryUpdateModel(entryToUpdate,
                               new string[] { "Name", "Message", "DateAdded" }))
            {
                _repository.UpdateEntry();
                return(RedirectToAction("Index"));
            }
            else
            {
                return(View(entryToUpdate));
            }
            // se la validazione dei dati inseriti fallisce torna alla vista di inserimento dove l'utente può correggerli
        }
        public async Task <IActionResult> Post([FromForm] GuestbookEntry entry)
        {
            _logger.LogInformation($"Calling backend at {_envConfig.BackendAddress} for message authored by {entry.Name}");

            try
            {
                var httpClient = _factory.CreateClient();
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                await httpClient.PostAsJsonAsync <GuestbookEntry>(_envConfig.BackendAddress, entry);

                return(RedirectToAction("Index"));
            }
            catch (Exception e)
            {
                _logger.LogError(e.ToString());
                return(View());
            }
        }
Beispiel #24
0
        private GuestbookEntry GetGuestbookEntryFromMarkdown(string filePath)
        {
            var entry = new GuestbookEntry
            {
                GitHubUsername = Path.GetFileNameWithoutExtension(filePath)
            };

            var lines = File.ReadLines(filePath);

            foreach (var line in lines.Where(x => !string.IsNullOrWhiteSpace(x) && x.Contains(":")))
            {
                var colon   = line.IndexOf(':');
                var prefix  = line.Substring(0, colon).Trim();
                var content = line.Substring(colon + 1).Trim();
                entry.SetProperty(prefix, content);
            }

            return(entry);
        }
Beispiel #25
0
 public void SaveEntry(GuestbookEntry entry)
 {
     entry.Id          = ++_entryId;
     entry.DateCreated = DateTime.Now;
     _entries.Add(entry);
 }
 public void Post([FromBody] GuestbookEntry entry)
 {
     _guestbookService.SaveEntry(entry);
 }
 public EntryAddedEvent(int guestbookId, GuestbookEntry entry)
 {
     GuestbookId = guestbookId;
     Entry       = entry;
 }
Beispiel #28
0
 public void AddEntry(GuestbookEntry entry)
 {
     entry.DateAdded = DateTime.Now;
     _db.Entries.Add(entry);
     _db.SaveChanges();
 }
Beispiel #29
0
 public void UpdateEntry(GuestbookEntry entry)
 {
 }
 public ActionResult Post(GuestbookEntry entry)
 {
     TempData["message"] = "Thanks for posting!";
     _repository.AddEntry(entry);
     return(RedirectToAction("Index"));
 }