Exemple #1
0
        public IHttpActionResult SubmitQuestion(SubmitQuestionRequest submitQuestionRequest)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var questionTags = new List <QuestionTag>();

            foreach (var tag in submitQuestionRequest.QuestionTags)
            {
                var t = HackDbContext.QuestionTags.SingleOrDefault(x => x.Tag.Equals(tag.Name));
                if (t != null)
                {
                    questionTags.Add(t);
                }
            }

            var question = new Question(submitQuestionRequest.Title, submitQuestionRequest.Description, questionTags,
                                        DateTime.UtcNow, ApplicationContext.User.UserId);

            HackDbContext.Questions.Add(question);
            HackDbContext.SaveChanges();

            return(Ok(new SubmitQuestionResponse(question.Id)));
        }
Exemple #2
0
 public async Task <Room> GetRoomBySparkRoomID(string sparkRoomId)
 {
     using (var db = new HackDbContext())
     {
         return(await db.Rooms.FirstOrDefaultAsync(r => r.RoomId == sparkRoomId));
     }
 }
Exemple #3
0
 public async Task <IEnumerable <Meeting> > GetMeetingsByRoomId(long roomId)
 {
     using (var db = new HackDbContext())
     {
         return(await db.Meetings.Where(m => m.RoomId == roomId).ToListAsync());
     }
 }
        private async Task <bool> UpdateAnswer(int qnum, string msg, HackDbContext db, Meeting meet, string userId,
                                               string roomId)
        {
            var answer = await db.Answers.FirstOrDefaultAsync(a => a.MeetingId == meet.Id && a.UserId == userId);

            if (answer == null)
            {
                throw new Exception($"userId={userId} not found in roomId={roomId}");
            }
            switch (qnum)
            {
            case 1:
                answer.Ans1 = msg;
                break;

            case 2:
                answer.Ans2 = msg;
                break;

            case 3:
                answer.Ans3 = msg;
                break;

            default:
                throw new ArgumentException($"invalid question number={qnum}");
            }
            db.Entry(answer).State = EntityState.Modified;
            await db.SaveChangesAsync();

            return(string.IsNullOrEmpty(answer.Ans1) ||
                   string.IsNullOrEmpty(answer.Ans2) ||
                   string.IsNullOrEmpty(answer.Ans3));
        }
Exemple #5
0
 public async Task <bool> IsExistRoomSparkId(string sparkRoomId)
 {
     using (var db = new HackDbContext())
     {
         return(await db.Rooms.AnyAsync(r => r.RoomId == sparkRoomId));
     }
 }
 public async Task <IEnumerable <Meeting> > GetOpenMeetingsByRoomId(long roomId)
 {
     using (var db = new HackDbContext(ConfigService.Get(Constants.DbConnectionKey)))
     {
         return(await db.Meetings.Where(m => m.RoomId == roomId && m.DateEnd == null).ToListAsync());
     }
 }
Exemple #7
0
        public async Task <string> GetSparkRoomId(long roomId)
        {
            using (var db = new HackDbContext(ConfigService.Get(Constants.DbConnectionKey)))
            {
                var room = await db.Rooms.FindAsync(roomId);

                return(room == null ? string.Empty : room.RoomId);
            }
        }
 public async Task <Answer> GetNextMeetingPushAsync(long meetId)
 {
     using (var db = new HackDbContext(ConfigService.Get(Constants.DbConnectionKey)))
     {
         return(await db.Answers
                .Where(a => a.MeetingId == meetId && a.Ans1 == null && a.Ans2 == null && a.Ans3 == null)
                .OrderBy(a => a.Id)
                .FirstOrDefaultAsync());
     }
 }
        public async Task <int> DeleteAnswersInMiting(long meetId)
        {
            using (var db = new HackDbContext(ConfigService.Get(Constants.DbConnectionKey)))
            {
                var anss = await db.Answers.Where(a => a.MeetingId == meetId).ToListAsync();

                db.Answers.RemoveRange(anss);
                return(await db.SaveChangesAsync());
            }
        }
Exemple #10
0
        public IHttpActionResult LeaveQuestion(long id)
        {
            var question = HackDbContext.Questions.SingleOrDefault(x => x.Id == id);

            if (question == null)
            {
                return(InternalServerError());
            }

            question.PeopleEnteredInQuestion = false;
            HackDbContext.SaveChanges();

            return(Ok());
        }
        public async Task <Room> FindRoom(long roomId)
        {
            using (var db = new HackDbContext(ConfigService.Get(Constants.DbConnectionKey)))
            {
                var room = await db.Rooms.FirstOrDefaultAsync(r => r.Id == roomId);

                if (room == null)
                {
                    throw new ApplicationException("not found roomId=" + roomId);
                }

                return(room);
            }
        }
Exemple #12
0
        public async Task <Room> AddNewRoom(Room room)
        {
            if (room == null)
            {
                return(null);
            }
            using (var db = new HackDbContext())
            {
                var r = db.Rooms.Add(room);
                await db.SaveChangesAsync();

                return(r);
            }
        }
        public IHttpActionResult SubmitOffer(SubmitOfferRequest submitOfferRequest)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var offer = new Offer(submitOfferRequest.Text, DateTime.UtcNow, submitOfferRequest.VideoCallAvailable,
                                  ApplicationContext.User.UserId, submitOfferRequest.QuestionForId);

            HackDbContext.Offers.Add(offer);
            HackDbContext.SaveChanges();

            return(Ok());
        }
Exemple #14
0
        public IHttpActionResult PollQuestion()
        {
            var questions = HackDbContext.Questions.Where(x => x.UserId == ApplicationContext.User.UserId && x.PeopleEnteredInQuestion);

            var response = new List <PollQuestion>();

            foreach (var question in questions)
            {
                response.Add(new PollQuestion(question.Id, question.Title, question.Description));
                question.PeopleEnteredInQuestion = false;
            }
            HackDbContext.SaveChanges();

            return(Ok(new PollResponse(response)));
        }
        public async Task <IEnumerable <Answer> > GetLastMeetingAnswersByRoomId(long roomId)
        {
            using (var db = new HackDbContext(ConfigService.Get(Constants.DbConnectionKey)))
            {
                var lastMeet = await db.Meetings
                               .Where(m => m.RoomId == roomId && m.DateEnd != null)
                               .OrderByDescending(m => m.Id)
                               .FirstOrDefaultAsync();

                if (lastMeet == null)
                {
                    return(new List <Answer>());
                }
                return(await db.Answers.Where(a => a.MeetingId == lastMeet.Id).ToListAsync());
            }
        }
        public async Task <Meeting> UpdateMeeting(long meetId, Meeting meet)
        {
            using (var db = new HackDbContext(ConfigService.Get(Constants.DbConnectionKey)))
            {
                var dbMeet = await db.Meetings.FirstOrDefaultAsync(m => m.Id == meetId);

                if (dbMeet == null)
                {
                    return(null);
                }
                dbMeet.DateEnd         = meet.DateEnd;
                dbMeet.DateStart       = meet.DateStart;
                db.Entry(dbMeet).State = EntityState.Modified;
                await db.SaveChangesAsync();

                return(dbMeet);
            }
        }
        public IHttpActionResult Login(LoginRequest loginRequest)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var user = HackDbContext.Users.SingleOrDefault(x => x.Username.Equals(loginRequest.Username) && x.Password.Equals(loginRequest.Password));

            if (user == null)
            {
                return(Unauthorized());
            }

            user.GenerateToken();
            HackDbContext.SaveChanges();

            return(Ok(new LoginResponse(user.Token)));
        }
        public IHttpActionResult Logout(LogoutRequest logoutRequest)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var user = HackDbContext.Users.SingleOrDefault(x => x.Username.Equals(logoutRequest.Username));

            if (user == null)
            {
                return(InternalServerError());
            }

            user.Token = string.Empty;
            HackDbContext.SaveChanges();

            return(Ok());
        }
        public async Task <Meeting> SaveMeeting(Room room, IEnumerable <ChatRoomUser> users)
        {
            using (var db = new HackDbContext(ConfigService.Get(Constants.DbConnectionKey)))
            {
                using (var transaction = db.Database.BeginTransaction())
                {
                    try
                    {
                        var meet = new Meeting {
                            DateStart = DateTime.UtcNow, RoomId = room.Id
                        };

                        meet = db.Meetings.Add(meet);
                        await db.SaveChangesAsync();

                        var prepareAnswers = users.Select(u => new Answer
                        {
                            MeetingId = meet.Id,
                            UserId    = u.userId,
                            UserName  = u.userName.Split(' ').FirstOrDefault()
                        }).ToList();

                        db.Answers.AddRange(prepareAnswers);
                        await db.SaveChangesAsync();

                        transaction.Commit();

                        return(meet);
                    }
                    catch
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }
Exemple #20
0
 protected BaseController(HackDbContext hackDbContext, IApplicationContext applicationContext)
 {
     HackDbContext      = hackDbContext;
     ApplicationContext = applicationContext;
 }
        public async Task <IHttpActionResult> Get(int qnum, string id, string msg)
        {
            Trace.TraceInformation($"Incoming answer from {id}, {qnum}: {msg}");

            if (string.IsNullOrEmpty(id) || string.IsNullOrEmpty(msg))
            {
                return(BadRequest());
            }
            var s = id.Split('-');

            if (s.Length != 2)
            {
                return(BadRequest());
            }

            var roomId = s[0];
            var userId = s[1];

            using (var db = new HackDbContext())
            {
                using (var transaction = db.Database.BeginTransaction())
                {
                    try
                    {
                        var room = await db.Rooms.FirstOrDefaultAsync(r => r.RoomId == roomId);

                        if (room == null)
                        {
                            return(BadRequest($"roomId={roomId} not found"));
                        }
                        var meet = await db.Meetings.FirstOrDefaultAsync(m => m.RoomId == room.Id && m.DateEnd == null);

                        if (meet == null)
                        {
                            return(BadRequest($"open meeting not found in roomId={roomId}"));
                        }

                        var isNotComplete = await UpdateAnswer(qnum, msg, db, meet, userId, roomId);

                        if (isNotComplete)
                        {
                            transaction.Commit();
                            return(Ok(Helpers.CreateExternalMessage())); //exist not answered
                        }

                        //init next push or end meeting
                        var nextAnswer = await _metingAnswersRepository.GetNextMeetingPushAsync(meet.Id);

                        if (nextAnswer == null)
                        {
                            // end meeting
                            //set endtime
                            meet.DateEnd         = DateTime.Now;
                            db.Entry(meet).State = EntityState.Modified;
                            await db.SaveChangesAsync();

                            //call remote api
                            await _connectorClient.PushEndOfMeetingAsync(room.BotName, $"{room.RoomId}-{userId}");
                        }
                        else
                        {
                            //nextAnswer.Meeting.Room.RoomId
                            await _connectorClient.PushRemoteChatService(room.BotName,
                                                                         $"{room.RoomId}-{nextAnswer.UserId}", nextAnswer.UserName);
                        }
                        transaction.Commit();
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        var e = ex.GetInnerBottomException();
                        Trace.TraceError(e.ToString());
                        return(ResponseMessage(Request.CreateResponse(
                                                   HttpStatusCode.InternalServerError, Helpers.CreateExternalMessage(e.Message))));
                    }
                }
            }

            return(Ok(Helpers.CreateExternalMessage()));
        }
 public ProfileController(HackDbContext hackDbContext, IApplicationContext applicationContext)
     : base(hackDbContext, applicationContext)
 {
 }
 public QuestionTagController(HackDbContext hackDbContext, IApplicationContext applicationContext)
     : base(hackDbContext, applicationContext)
 {
 }
 public AccountApiController(HackDbContext hackDbContext, IApplicationContext applicationContext)
     : base(hackDbContext, applicationContext)
 {
 }
 public OfferApiController(HackDbContext hackDbContext, IApplicationContext applicationContext)
     : base(hackDbContext, applicationContext)
 {
 }
 public UserHelper(HackDbContext hackDbContext)
 {
     _hackDbContext = hackDbContext;
 }