private OperationResult <EventCategoryDto> CreateCategory(EventCategoryDto category) { var domain = Mapper.Map <EventCategory>(category); var result = _repository.Create <EventCategory>(domain); _repository.Save(); return(OperationResult.Success(Mapper.Map <EventCategoryDto>(result))); }
public OperationResult <EventCategoryDto> Save(EventCategoryDto eventCategory) { if (eventCategory == null) { throw new ArgumentNullException(nameof(eventCategory)); } var localizationResults = new List <OperationResult <EventCategoryLocalizationDto> >(); var categoriesResult = (eventCategory.Id > 0) ? UpdateCategory(eventCategory) : CreateCategory(eventCategory); if (!categoriesResult.Succeeded) { return(categoriesResult); } foreach (var localization in eventCategory.EventCategoryLocalizations) { localization.EventCategoryId = categoriesResult.Data.Id; var existed = _repository.GetById <EventCategoryLocalization>(localization.EventCategoryId, localization.LanguageId); var localizationResult = (existed != null) ? UpdateLocalization(existed, localization) : CreateLocalization(localization); localizationResults.Add(localizationResult); } var errors = localizationResults .Where(c => c.Errors != null) .SelectMany(c => c.Errors) .ToArray(); if (errors.Length > 0) { return(OperationResult.Fail <EventCategoryDto>(errors)); } var result = categoriesResult.Data; result.EventCategoryLocalizations = localizationResults .Where(c => c.Data != null) .Select(c => c.Data) .ToList(); return(OperationResult.Success(result)); }
public IHttpActionResult CreateEventCategory(EventCategoryDto eventCategoryDto) { if (!ModelState.IsValid) return BadRequest(); var eventCategory = Mapper.Map<EventCategoryDto, EventCategory>(eventCategoryDto); _cms.EventCategories.Add(eventCategory); _cms.SaveChanges(); eventCategoryDto.EventCategoryId = eventCategory.EventCategoryId; return Created(new Uri(Request.RequestUri + "/" + eventCategory.EventCategoryId), eventCategoryDto); }
public IHttpActionResult UpdateEventCategory(int id, EventCategoryDto eventCategoryDto) { if (!ModelState.IsValid) return BadRequest(); var eventCategoryInDb = _cms.EventCategories.SingleOrDefault(d => d.EventCategoryId == id); if (eventCategoryInDb == null) return NotFound(); Mapper.Map(eventCategoryDto, eventCategoryInDb); _cms.SaveChanges(); return Ok(); }
private static void TransformToModel(EventCategoryDto source, EventCategoryMvcViewModel destination) { var localizations = source.EventCategoryLocalizations; if (localizations == null || localizations.Count == 0) { return; } var ruLocalization = localizations.FirstOrDefault(c => c.LanguageId == (int)CultureLanguage.RU); var enLocalization = localizations.FirstOrDefault(c => c.LanguageId == (int)CultureLanguage.EN); destination.NameRu = ruLocalization?.Name; destination.NameEn = enLocalization?.Name; }
private OperationResult <EventCategoryDto> UpdateCategory(EventCategoryDto category) { var domain = _repository.GetById <EventCategory>(category.Id); if (domain == null) { return(OperationResult.Fail <EventCategoryDto> ($"Не удалось найти категорию (id = {category.Id})")); } domain = Mapper.Map(category, domain); var result = _repository.Update(domain); _repository.Save(); return(OperationResult.Success(Mapper.Map <EventCategoryDto>(result))); }
private static void Localize(EventCategoryDto source, EventCategoryMvcViewModel destination) { var language = LanguageHelper.GetThreadLanguage(); switch (language) { case CultureLanguage.EN: destination.LocalizedName = source.EventCategoryLocalizations .FirstOrDefault(c => c.LanguageId == 1)?.Name; break; case CultureLanguage.Undefined: case CultureLanguage.RU: case null: default: destination.LocalizedName = source.EventCategoryLocalizations .FirstOrDefault(c => c.LanguageId == 2)?.Name; break; } }
public async Task <IActionResult> Create(EventCategoryDto eventCategoryDto) { try { if (eventCategoryDto.Id > 0) { var eventCategory = await _unitOfWork.EventCategories.GetByIdAsync(eventCategoryDto.Id); if (eventCategory == null) { return(NotFound()); } _mapper.Map(eventCategoryDto, eventCategory); _unitOfWork.EventCategories.Update(eventCategory); await _unitOfWork.SaveAsync(); return(NoContent()); } else { var eventCategory = _mapper.Map <EventCategory>(eventCategoryDto); eventCategory.CreatedAt = DateTime.Now; await _unitOfWork.EventCategories.AddAsync(eventCategory); if (await _unitOfWork.SaveAsync()) { return(StatusCode(201)); } return(StatusCode(401)); } } catch (Exception ex) { throw new Exception("Creating eventBooking failed on saver"); } }
private static void TransformToDto(EventCategoryMvcViewModel source, EventCategoryDto destination) { var enLocalization = string.IsNullOrEmpty(source.NameEn) ? null : new EventCategoryLocalizationDto { EventCategoryId = source.Id, LanguageId = (int)CultureLanguage.EN, Name = source.NameEn }; var ruLocalization = string.IsNullOrEmpty(source.NameRu) ? null : new EventCategoryLocalizationDto { EventCategoryId = source.Id, LanguageId = (int)CultureLanguage.RU, Name = source.NameRu }; var localizations = new List <EventCategoryLocalizationDto>(); if (enLocalization != null) { localizations.Add(enLocalization); } if (ruLocalization != null) { localizations.Add(ruLocalization); } destination.EventCategoryLocalizations = (localizations.Count > 0) ? localizations : null; }