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); }
public async Task <RessourceType> Get(string id) { var ressourceType = await _ressourceTypeRepository.GetById(id); return(ressourceType); }