Esempio n. 1
0
        /// <summary>
        /// Creates a new space.
        /// </summary>
        /// <param name="name">The name of the space-to-be.</param>
        /// <returns>The newly created space.</returns>
        public async Task <ISpace> CreateSpace(SpaceName name)
        {
            var space = _spaceFactory.NewSpace(name);
            await _spaceRepository.Add(space);

            return(space);
        }
        public async Task <Space> Create(Space spaceIn)
        {
            var ressourceTypeIn = await _ressourceTypeRepository.GetById(spaceIn.SpaceTypeId);

            if (ressourceTypeIn == null)
            {
                throw new RessourceTypeRepositoryException(string.Format(_errorHandler.GetMessage(ErrorMessagesEnum.NotFound),
                                                                         nameof(RessourceType), spaceIn.SpaceTypeId), nameof(spaceIn.SpaceTypeId));
            }
            try
            {
                ressourceTypeIn.Count++; // Increamenting count when adding an asset
                await _ressourceTypeRepository.Update(ressourceTypeIn);

                spaceIn.SpaceTypeName = ressourceTypeIn.Name;
                await _spaceRepository.Add(spaceIn);
            }
            catch (MongoWriteException mwx)
            {
                if (mwx.WriteError.Category == ServerErrorCategory.DuplicateKey)
                {
                    var     pattern         = @"\{(?:[^{*}])*\}";
                    Match   result          = Regex.Match(mwx.Message, pattern);                     // get the dublicated feild from the string error msg
                    JObject duplicatedField = JsonConvert.DeserializeObject <JObject>(result.Value); // parse it  to get the field
                    throw new SpaceRepositoryException(string.Format(_errorHandler.GetMessage(ErrorMessagesEnum.DuplicateKey),
                                                                     nameof(Space), duplicatedField.First.Path), duplicatedField.First.Path);
                }
            }
            return(spaceIn);
        }