public IHttpActionResult AddContact([FromUri] string sessionKey, Shared.Dto.Contact contact) { var contactService = new ContactService(); if (contactService.SendContact(sessionKey, contact)) { return(Ok()); } return(Unauthorized()); }
public bool SendContact(string sessionKey, Shared.Dto.Contact contact) { var dbContext = DbContextFactory.GetContext(); var sessionService = new SessionService(); var user = sessionService.GetUser(sessionKey); if (user == null) { return(false); } User recipient; try { recipient = dbContext.Users.Single(u => u.Id == contact.ReceiverId); } catch (Exception) { return(false); } var newContact = new Contact { PublicKey = contact.PublicKey, Name = contact.Name, Sender = user, Recipient = recipient }; dbContext.Add(newContact); dbContext.SaveChanges(); var userService = new PushService(); userService.Push(recipient, "Recieved new contact!"); return(true); }