Exemplo n.º 1
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());
        }