示例#1
0
        public async Task <IHttpActionResult> DeleteRoom(long id)
        {
            Room room = db.Rooms.Find(id);

            if (room == null)
            {
                return(NotFound());
            }

            var meets = await _meetingService.GetMeetingsByRoomId(room.Id);

            foreach (var meet in meets)
            {
                await _metingAnswersRepository.DeleteAnswersInMiting(meet.Id);
            }

            var ids        = meets.Select(m => m.Id).ToList();
            var toDelMeets = await db.Meetings.Where(m => ids.Contains(m.Id)).ToListAsync();

            db.Meetings.RemoveRange(toDelMeets);
            await db.SaveChangesAsync();

            db.Rooms.Remove(room);
            await db.SaveChangesAsync();

            return(Ok(room));
        }
        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));
        }
        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());
            }
        }
示例#4
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 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;
                    }
                }
            }
        }
        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 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()));
        }