public ServiceResult <HomeworkDto> CreateHomework(HomeworkRequest homeworkRequest) { string validationMessage = ValidateHomeworkRequest(homeworkRequest); if (!string.IsNullOrEmpty(validationMessage)) { return(errorService.Error <HomeworkDto>(validationMessage)); } if (flytDexContext.Homeworks.Any(h => h.Id == homeworkRequest.Id && h.SchoolId == homeworkRequest.SchoolId && h.LessonId == homeworkRequest.LessonId)) { return(errorService.Error <HomeworkDto>("An error occurred: A homework with this Id already exists")); } //ICollection<LinkHomeworkResource> linkHomeworkResources = mapper.Map<ICollection<LinkHomeworkResourceDto>, ICollection<LinkHomeworkResource>>(homeworkRequest.LinkHomeworkResources); //ICollection<LinkStudentHomework> linkStudentHomeworks = mapper.Map<ICollection<LinkStudentHomeworkDto>, ICollection<LinkStudentHomework>>(homeworkRequest.LinkStudentHomeworks); Homework homework = mapper.Map <HomeworkRequest, Homework>(homeworkRequest); //homework.LinkHomeworkResources = linkHomeworkResources; flytDexContext.Homeworks.Add(homework); if (flytDexContext.SaveChanges() < 0) { return(errorService.Error <HomeworkDto>("An error occurred: Unable to save changes")); } HomeworkDto homeworkDto = mapper.Map <Homework, HomeworkDto>(homework); return(new ServiceResult <HomeworkDto>(homeworkDto, ResultType.Success, "Success")); }
public ServiceResult <Guid> AddUser(string forename, string surname, string password, UserType userType, IEnumerable <AuthorizationRoleType> roles, string email, Guid schoolId) { if (string.IsNullOrEmpty(forename) || string.IsNullOrEmpty(surname)) { return(errorService.Error <Guid>("Forename and Surname must be provided")); } if (string.IsNullOrEmpty(password)) { return(errorService.Error <Guid>("Password must be provided")); } string username = GenerateUniqueUsername(forename, surname, userType, email, schoolId); string hashedPassword = authorizationService.GeneratePassword(password); string alexaPassCode = authorizationService.GenerateAlexaPassCode(); User user = new User(username, hashedPassword, forename, alexaPassCode); flytDexContext.Users.Add(user); if (flytDexContext.SaveChanges() <= 0) { return(errorService.Error <Guid>("Error adding user, see log for error message")); } if (applicationRoleService.AddRolesToUser(user, roles).ResultType == ResultType.Error) { return(errorService.Warn <Guid>(string.Format("Failed To add user roles for user id {0}", user.Id))); } return(new ServiceResult <Guid>(user.Id, ResultType.Success, "Success")); }
public ServiceResult <PeriodDto> CreatePeriod(bool save, Guid schoolId, string name, DateTime startDateTime, DateTime endDateTime) { Period period = new Period() { SchoolId = schoolId, StartDateTime = startDateTime, Day = startDateTime.DayOfWeek.ToString(), EndDateTime = endDateTime, Name = name }; flytDexContext.Periods.Add(period); if (save) { if (flytDexContext.SaveChanges() == -1) { return(errorService.Error <PeriodDto>("Error adding period")); } } PeriodDto periodDto = mapper.Map <PeriodDto>(period); return(new ServiceResult <PeriodDto>(periodDto)); }