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()));
        }
Ejemplo n.º 2
0
 public async Task <Answer> GetNextMeetingPushAsync(long meetId)
 {
     return(await _metingAnswersRepository.GetNextMeetingPushAsync(meetId));
 }