Ejemplo n.º 1
0
        public Reply Save([FromBody] Models.Requests.Room room)
        {
            Reply reply = new Reply();

            try
            {
                using (ChatPruebaTecnicaDBEntities db = new ChatPruebaTecnicaDBEntities())
                {
                    Models.Room modelRoom = new Models.Room
                    {
                        name         = room.Name,
                        description  = room.Description,
                        date_created = DateTime.Now,
                        idState      = 1
                    };

                    db.Rooms.Add(modelRoom);
                    reply.Result = db.SaveChanges();
                }
            }
            catch (Exception e)
            {
                reply.Result  = 0;
                reply.Message = "Intentalo más tarde.";
                reply.Data    = e.InnerException;
            }

            return(reply);
        }
Ejemplo n.º 2
0
        public Reply Get()
        {
            Reply reply = new Reply();

            try
            {
                using (ChatPruebaTecnicaDBEntities db = new ChatPruebaTecnicaDBEntities())
                {
                    List <Models.Requests.Room> rooms = (from d in db.Rooms
                                                         where d.idState == 1
                                                         select new Models.Requests.Room
                    {
                        Id = d.id,
                        Name = d.name,
                        Description = d.description
                    }).ToList();
                    reply.Result = 1;
                    reply.Data   = rooms;
                }
            }
            catch (Exception e)
            {
                reply.Result  = 0;
                reply.Message = "Intentalo más tarde.";
                reply.Data    = e.InnerException;
            }

            return(reply);
        }
        public Reply GetMessages([FromBody] MessageRequest request)
        {
            Reply reply = new Reply();
            List <MessageResponse> messages = new List <MessageResponse>();

            try
            {
                using (ChatPruebaTecnicaDBEntities db = new ChatPruebaTecnicaDBEntities())
                {
                    messages = (from d in db.Messages.ToList()
                                where d.idState == 1 && d.idRoom == request.IdRoom
                                orderby d.date_created descending
                                select new MessageResponse
                    {
                        Message = d.text,
                        Id = d.id,
                        IdUser = d.idUser,
                        NickName = d.User.nickName,
                        DateCreated = d.date_created,
                        TypeMessage = (
                            new Func <int>(
                                () =>
                        {
                            try
                            {
                                if (d.idUser == request.IdUser)
                                {
                                    return(1);
                                }
                                else
                                {
                                    return(2);
                                }
                            }
                            catch
                            {
                                return(2);
                            }
                        }
                                )()
                            )
                    }).ToList();
                    reply.Result = 1;
                    reply.Data   = messages;
                }
            }
            catch (Exception e)
            {
                reply.Result  = 0;
                reply.Message = "Intentalo más tarde.";
                reply.Data    = e.InnerException;
            }

            return(reply);
        }
Ejemplo n.º 4
0
        public Reply Get()
        {
            Reply reply = new Reply();

            using (ChatPruebaTecnicaDBEntities db = new ChatPruebaTecnicaDBEntities())
            {
                List <UserViewModel> lista = (from user in db.Users
                                              select new UserViewModel
                {
                    NickName = user.nickName
                }).ToList();
                reply.Data   = lista;
                reply.Result = 1;
            }

            return(reply);
        }
        public void Send(int idRoom, int idUser, string userName, string message)
        {
            var date = DateTime.Now.ToString();

            using (ChatPruebaTecnicaDBEntities db = new ChatPruebaTecnicaDBEntities())
            {
                var oMessage = new Message();
                oMessage.idRoom       = idRoom;
                oMessage.date_created = DateTime.Now;
                oMessage.idUser       = idUser;
                oMessage.text         = message;
                oMessage.idState      = 1;

                db.Messages.Add(oMessage);
                db.SaveChanges();
            }
            Clients.Group(idRoom.ToString()).sendChat(userName, message, date, idUser);
        }
Ejemplo n.º 6
0
        public Reply Register([FromBody] Models.Requests.User user)
        {
            Reply reply = new Reply();

            try
            {
                using (ChatPruebaTecnicaDBEntities db = new ChatPruebaTecnicaDBEntities())
                {
                    var userExist = db.Users.Any(x => x.nickName == user.NickName && x.idState == 1);
                    if (!userExist)
                    {
                        Models.User modelUser = new User
                        {
                            nickName     = user.NickName,
                            date_created = DateTime.Now,
                            idState      = 1
                        };

                        db.Users.Add(modelUser);
                        reply.Result = db.SaveChanges();
                        reply.Data   = new UserViewModel
                        {
                            Id       = modelUser.id,
                            NickName = modelUser.nickName
                        };
                    }
                    else
                    {
                        reply.Result  = 0;
                        reply.Message = "El nombre de usuario existe y se encuentra activo. Intenta más tarde.";
                    }
                }
            }
            catch (Exception e)
            {
                reply.Result  = 0;
                reply.Message = "Intentalo más tarde.";
                reply.Data    = e.InnerException;
            }

            return(reply);
        }