public static PostFullModel Convert(Post post) { PostFullModel model = new PostFullModel { PostId = post.Id, Date = post.Date, Content = post.Content, IsFile = post.IsFile, User = new UserFullModel { UserId = post.User.Id, Picture = post.User.Picture, Username = post.User.Username, IsOnline =post.User.IsOnline, PostCount = post.User.Posts.Count, ChatRoomCount = post.User.ChatRooms.Count }, ChatRoom = new ChatRoomModel { ChatRoomId = post.ChatRoom.Id, Name = post.ChatRoom.Name, PostCount = post.ChatRoom.Posts.Count, UserCount = post.ChatRoom.Users.Count } }; return model; }
public static PostModel Convert(Post post) { PostModel model = new PostModel { PostId = post.Id, Date = post.Date, Content = post.Content, IsFile = post.IsFile, UserId = post.UserId, ChatRoomId = post.ChatRoomId }; return model; }
static void Main() { //Create and initialize the database Database.SetInitializer(new MigrateDatabaseToLatestVersion <ChatContext, Chat.Data.Migrations.Configuration>()); var context = new ChatContext(); using (context) { var user = new User { Username = "******", Password = "******", Picture = "....." }; context.Users.Add(user); var chatRoom = new ChatRoom { Name = "chatroom", Users = new User[] { user } }; context.ChatRooms.Add(chatRoom); var post = new Post { Date = DateTime.Now, UserId = user.Id, ChatRoomId = chatRoom.Id, Content = "ok" }; context.Posts.Add(post); context.SaveChanges(); } }
public HttpResponseMessage Post(Post model) { if (model.Date == null || model.UserId == 0 || model.ChatRoomId == 0 || model.Content == null) { var errResponse = this.Request.CreateErrorResponse( HttpStatusCode.BadRequest, "Post shoul have Date, UserId, ChatRoomId, Content"); throw new HttpResponseException(errResponse); } var entity = this.postRepository.Add(model); PubNubConsole.Publish(string.Format("New message from {0} in {1} chat room", entity.UserId, entity.ChatRoomId)); var response = Request.CreateResponse(HttpStatusCode.Created, entity); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = entity.Id })); return response; }
public void Delete(Post model) { this.postRepository.Delete(model); }
public HttpResponseMessage Put(int id, Post model) { if (model.Content == null) { var errResponse = this.Request.CreateErrorResponse( HttpStatusCode.BadRequest, "Post Content could not be null"); throw new HttpResponseException(errResponse); } var entity = this.postRepository.Get(id); entity = this.postRepository.Update(id, model); var response = Request.CreateResponse(HttpStatusCode.OK, entity); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = entity.Id })); return response; }