Ejemplo n.º 1
0
 public static Person Create()
 {
     var person = new Person
         {
             FirstName = "Senior",
             LastName = "Konsulent",
             Email = "*****@*****.**",
             Title = "Seniorkonsulent",
             ImageUrl = "http://www.webstep.no/wp-content/uploads/2010/02/Ikon_bildeerstatt8-110x150.png"
         };
     return person;
 }
        public void DownloadPersonInfo(Person person)
        {
            var web = new HtmlWeb();
            var doc = web.Load(person.InfoUrl);

            // Get info entry
            var result = doc.DocumentNode.Descendants("div").First(x => x.Attributes.Contains("class") && x.Attributes["class"].Value.Contains("entry"));
            var info = result.InnerText.Replace("\t", String.Empty).Replace("\r", String.Empty).Replace("Last ned vCard", "");
            person.Info = info;
            Messenger.Default.Send(new PersonInfoRetrievedEvent()
                {
                    Person = person,
                    Info = info
                });
        }
 public void Delete(Person person)
 {
     var request = new RestRequest(Method.DELETE) { RequestFormat = DataFormat.Json };
     request.AddParameter("Id", person.Id);
     _client.ExecuteAsync<Person>(request, response =>
     {
         if (response.StatusCode == HttpStatusCode.OK)
         {
             App.Current.Dispatcher.BeginInvoke(new Action(() => Messenger.Default.Send(new PersonDeletedEvent { Person = response.Data })));
         }
         else
         {
             App.Current.Dispatcher.BeginInvoke(new Action(() => Messenger.Default.Send(new CommunicationFailedEvent() { Message = "Failed to delete person" })));
         }
     });
 }
 public void AddPerson(Person person)
 {
     var request = new RestRequest(Method.POST) { RequestFormat = DataFormat.Json };
     request.AddBody(person);
     _client.ExecuteAsync<Person>(request, response =>
     {
         if (response.StatusCode == HttpStatusCode.Created)
         {
             App.Current.Dispatcher.BeginInvoke(new Action(() => Messenger.Default.Send(new PersonCreatedEvent{Person = response.Data})));
         }
         else
         {
             App.Current.Dispatcher.BeginInvoke(new Action(() => Messenger.Default.Send(new CommunicationFailedEvent() { Message = "Failed to Add person" })));
         }
     });
 }
        // POST api/Person
        public HttpResponseMessage PostPerson(Person person)
        {
            if (ModelState.IsValid)
            {
                db.Persons.Add(person);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, person);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = person.Id }));
                return response;
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }
 public void UpdatePerson(Person person)
 {
     var request = new RestRequest(Method.PUT) { RequestFormat = DataFormat.Json };
     request.AddBody(person);
     var url = BaseUrl + "/" + person.Id;
     _client = new RestClient(url);
     _client.ExecuteAsync<Person>(request, response =>
     {
         if (response.StatusCode == HttpStatusCode.OK)
         {
             App.Current.Dispatcher.BeginInvoke(new Action(() => Messenger.Default.Send(new PersonUpdatedEvent { Person = response.Data })));
         }
         else
         {
             App.Current.Dispatcher.BeginInvoke(new Action(() => Messenger.Default.Send(new CommunicationFailedEvent { Message = "Failed to update person" })));
         }
     });
 }
        // PUT api/Person
        public HttpResponseMessage PutPerson(int id, Person person)
        {
            if (ModelState.IsValid)
            {
                db.Entry(person).State = EntityState.Modified;

                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }

                return Request.CreateResponse(HttpStatusCode.OK, person);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }
        private static Person Parse(HtmlNode personNode)
        {
            var person = new Person { Id = PersonService.GetId() };

            person.ImageUrl = personNode.Descendants("img").Select(x => x.GetAttributeValue("src", "")).First();
            var title = personNode.Descendants("em").First(node => node.Attributes.Contains("class") && node.Attributes["class"].Value == "title").
                    InnerText;
            person.Title = Clean(title);

            var name = personNode.Descendants("a").First();
            person.SplitName(name.InnerText);
            person.InfoUrl = name.GetAttributeValue("href", "");

            var email = personNode.Descendants("a").Skip(1).First();
            var decodedEmail = HttpUtility.HtmlDecode(email.Attributes["href"].Value);
            person.Email = decodedEmail.Replace("mailto:", "");

            var phone =
                personNode.Descendants("span").First(n => n.Attributes.Contains("class") && n.Attributes["class"].Value.Contains("value"));
            person.Phone = phone.InnerText;

            return person;
        }
 private void LoadPersonInfo(Person person)
 {
     _personService.DownloadPersonInfo(person);
 }
 public UpdatePersonViewModel(Person person)
 {
     Person = person;
 }
 public PersonViewModel(Person person)
 {
     Person = person;
 }