public ResultDTO Create(CreateClassDTO createClassDTO) { createClassDTO.Validate(); if (createClassDTO.Invalid) { return(new ResultDTO(false, "Invalid fields.", createClassDTO.Notifications)); } try { using (var transaction = _context.Database.BeginTransaction()) { var userExistis = _userRepository.Get(createClassDTO.Name); if (userExistis != null) { return(new ResultDTO(false, "User already exist!", null)); } var user = User.Create(createClassDTO.UserId, createClassDTO.Name, createClassDTO.Avatar, createClassDTO.Whatsapp, createClassDTO.Bio); _userRepository.Create(user); if (!_userRepository.Save()) { return(new ResultDTO(false, "Cannot be saved.", null)); } var classese = Class.Create(createClassDTO.ClassId, createClassDTO.Subject, createClassDTO.Cost, user.Id); _classRepository.Create(classese); if (!_classRepository.Save()) { return(new ResultDTO(false, "Cannot be saved.", null)); } foreach (var item in createClassDTO.Schedule) { var classSchedule = ClassSchedule.Create( item.ClassScheduleId, item.WeekDay, ConvertHours.ToMinute(item.From), ConvertHours.ToMinute(item.To), classese.Id); _classScheduleRepository.Create(classSchedule); } if (!_classRepository.Save()) { return(new ResultDTO(false, "Cannot be saved.", null)); } transaction.Commit(); return(new ResultDTO(true, "Successfuly registered.", null)); } } catch (Exception e) { return(new ResultDTO(false, "Cannot be saved.", e)); } }
public IActionResult Create( [FromServices] ICreateClassServices _createClassServices, [FromBody] CreateClassDTO createClassDTO) { var result = _createClassServices.Create(createClassDTO); if (result.Success) { return(Ok(result)); } return(BadRequest(result)); }