Example #1
0
        public string CreateRoom(string Room)
        {
            List <ErrorCodes> errors    = new List <ErrorCodes>();
            ErrorTypes        errorType = ErrorTypes.Ok;

            Room room;

            try
            {
                room = BsonSerializer.Deserialize <Room>(Room);
            }
            catch (Exception e)
            {
                errors.Add(ErrorCodes.StringIsNotJsonFormat);
                return(new Notification(null, ErrorTypes.Error, errors).ToJson());
            }
            //assign ID to room
            room.Id = ObjectId.GenerateNewId(DateTime.Now).ToString();

            //Check that the secret doesn't already exist
            var returnValue = BsonSerializer.Deserialize <Notification>(GetByUniqueSecret(room.Secret));

            if (returnValue.ErrorType == ErrorTypes.Ok)
            {
                errors.Add(ErrorCodes.RoomSecretAlreadyInUse);
                return(new Notification(null, ErrorTypes.Error, errors).ToJson());
            }

            string roomId;

            try
            {
                roomId = _rr.AddRoom(room);
            }
            catch (Exception e)
            {
                errors.Add(ErrorCodes.CouldNotAddRoomToDatabase);
                return(new Notification(null, ErrorTypes.Error, errors).ToJson());
            }
            //Publish to rabbitMQ after, because we need the id
            string error = String.Empty;

            try
            {
                _irabbitPublisher.publishString("CreateRoom", room.ToJson());
            }
            catch (Exception e)
            {
                errors.Add(ErrorCodes.RabbitMqError);
                errorType = ErrorTypes.Complicated;
            }

            return(new Notification(roomId, errorType, errors).ToJson());
        }
Example #2
0
        public string CreateChatMessage(string ChatMessage)
        {
            List <ErrorCodes> errors    = new List <ErrorCodes>();
            ErrorTypes        errorType = ErrorTypes.Ok;

            ChatMessage chatMsg;
            string      errorMsg = String.Empty;

            try
            {
                chatMsg = BsonSerializer.Deserialize <ChatMessage>(ChatMessage);
            }
            catch (Exception e)
            {
                errors.Add(ErrorCodes.CouldNotParseJsonToClass);
                return(new Notification(null, ErrorTypes.Error, errors).ToJson());
            }

            if (!_rr.DoesRoomExist(chatMsg.RoomId))
            {
                errors.Add(ErrorCodes.RoomDoesNotExist);
                return(new Notification(null, ErrorTypes.Error, errors).ToJson());
            }

            //Assign date to ChatMessage
            chatMsg.Timestamp = TimeHelper.timeSinceEpoch();



            //assign ID to room
            chatMsg.Id = ObjectId.GenerateNewId(DateTime.Now).ToString();
            try
            {
                _cr.AddChatMessage(chatMsg);
            }
            catch (Exception e)
            {
                errors.Add(ErrorCodes.CouldNotGetChatMessages);
                return(new Notification(null, ErrorTypes.Error, errors).ToJson());
            }
            try
            {
                _irabbitPublisher.publishString("CreateChatMessage", chatMsg.ToJson());
            }
            catch (Exception e)
            {
                errors.Add(ErrorCodes.RoomDoesNotExist);
                errorType = ErrorTypes.Complicated;
            }

            return(new Notification(null, errorType, errors).ToJson());
        }
Example #3
0
        public string CreateQuestion(string question, string type)
        {
            List <ErrorCodes> errors    = new List <ErrorCodes>();
            ErrorTypes        errorType = ErrorTypes.Ok;

            Type questionType;

            try
            {
                string typeString = "WisR.DomainModels." + type;
                questionType = Type.GetType(typeString);
            }
            catch (Exception e)
            {
                errors.Add(ErrorCodes.CouldNotGetQuestionType);
                return(new Notification(null, ErrorTypes.Error, errors).ToJson());
            }

            object   b;
            Question q;

            try
            {
                b = BsonSerializer.Deserialize(question, questionType);
                q = (Question)b;
            }
            catch (Exception e)
            {
                errors.Add(ErrorCodes.CouldNotParseJsonToClass);
                return(new Notification(null, ErrorTypes.Error, errors).ToJson());
            }
            if (q.Id != null)
            {
                errors.Add(ErrorCodes.NewQuestionIdShouldBeNull);
                return(new Notification(null, ErrorTypes.Error, errors).ToJson());
            }
            if (!_rr.DoesRoomExist(q.RoomId))
            {
                errors.Add(ErrorCodes.RoomDoesNotExist);
                return(new Notification(null, ErrorTypes.Error, errors).ToJson());
            }

            q.Id = ObjectId.GenerateNewId(DateTime.Now).ToString();

            q.CreationTimestamp = TimeHelper.timeSinceEpoch();
            q.ExpireTimestamp   = Convert.ToString(Convert.ToInt64(TimeHelper.timeSinceEpoch()) + Convert.ToInt64(q.ExpireTimestamp) * 60000);

            try
            {
                _irabbitPublisher.publishString("CreateQuestion", q.ToJson());
            }
            catch (Exception e)
            {
                errors.Add(ErrorCodes.RabbitMqError);
                errorType = ErrorTypes.Complicated;
            }

            try
            {
                _qr.AddQuestionObject(b);
            }
            catch (Exception)
            {
                errors.Add(ErrorCodes.CouldNotAddQuestion);
                return(new Notification(null, ErrorTypes.Error, errors).ToJson());
            }

            return(new Notification(null, errorType, errors).ToJson());
        }