public async Task <ActionResponse <StudentRegisterEntryDto> > UpdateEntry(StudentRegisterEntryInsertRequest request) { try { var entityToUpdate = unitOfWork.GetGenericRepository <StudentRegisterEntry>().FindSingle(request.EntryId.Value); entityToUpdate.Notes = request.Notes; entityToUpdate.EducationProgramId = request.EducationProgramId.Value; entityToUpdate.StudentRegisterId = request.BookId.Value; entityToUpdate.StudentRegisterNumber = request.StudentRegisterNumber.Value; entityToUpdate.EntryDate = request.EntryDate; entityToUpdate.ExamDate = request.ExamDate; entityToUpdate.ExamDateNumber = request.ExamDateNumber; unitOfWork.GetGenericRepository <StudentRegisterEntry>().Update(entityToUpdate); unitOfWork.Save(); return(await GetEntryById(request.EntryId.Value)); } catch (Exception) { return(await ActionResponse <StudentRegisterEntryDto> .ReturnError("Greška prilikom ažuriranja zapisa matične knjige.")); } finally { //await cacheService.RefreshCache<List<StudentRegisterEntryDto>>(); } }
public async Task <ActionResponse <StudentRegisterEntryDto> > UpdateEntry(StudentRegisterEntryInsertRequest request) { return(await studentRegisterService.UpdateEntry(request)); }
public async Task <ActionResponse <StudentRegisterEntryDto> > InsertEntry(StudentRegisterEntryInsertRequest request) { try { var transactionOptions = new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted, Timeout = TransactionManager.MaximumTimeout }; using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, transactionOptions, TransactionScopeAsyncFlowOption.Enabled)) { if ((await GetEntriesForStudentAndProgram(request)) .IsNotSuccess(out ActionResponse <List <StudentRegisterEntryDto> > checkResponse, out List <StudentRegisterEntryDto> alreadyExisting)) { scope.Dispose(); return(await ActionResponse <StudentRegisterEntryDto> .ReturnError(checkResponse.Message)); } if (alreadyExisting.Any()) { scope.Dispose(); return(await ActionResponse <StudentRegisterEntryDto> .ReturnError("Nemoguće unjeti novi zapis za kombinaciju studenta i edukacijskog programa, takav zapis već postoji.")); } if ((await GetEntryForStudentNumberAndBookNumberAndBookYear(request)) .IsNotSuccess(out ActionResponse <StudentRegisterEntryDto> registerEntryResponse, out StudentRegisterEntryDto existingEntry)) { scope.Dispose(); return(await ActionResponse <StudentRegisterEntryDto> .ReturnError(registerEntryResponse.Message)); } if (alreadyExisting.Any()) { scope.Dispose(); return(await ActionResponse <StudentRegisterEntryDto> .ReturnError("Nemoguće unjeti novi zapis jer zapis sa istim brojem studenta već postoji unutar izabrane knjige.")); } if ((await PrepareForInsert(request)) .IsNotSuccess(out ActionResponse <StudentRegisterEntryInsertRequest> prepareResponse, out request)) { scope.Dispose(); return(await ActionResponse <StudentRegisterEntryDto> .ReturnError(prepareResponse.Message)); } if (request.CreateNewBook) { var entityDto = new StudentRegisterDto { BookNumber = request.BookNumber, BookYear = request.BookYear, }; if ((await Insert(entityDto)).IsNotSuccess(out ActionResponse <StudentRegisterDto> bookInsertResponse, out entityDto)) { scope.Dispose(); return(await ActionResponse <StudentRegisterEntryDto> .ReturnError(bookInsertResponse.Message)); } request.BookId = entityDto.Id; } var studentInGroup = unitOfWork.GetGenericRepository <StudentsInGroups>() .ReadAllActiveAsQueryable() .Where(sig => sig.StudentId == request.StudentId.Value && sig.StudentGroupId == request.StudentGroupId) .FirstOrDefault(); if (studentInGroup == null) { scope.Dispose(); return(await ActionResponse <StudentRegisterEntryDto> .ReturnError("Specificirani student još ne postoji u grupi. Molimo prvo spremite studenta u grupu.")); } var entityToAdd = new StudentRegisterEntry { StudentRegisterId = request.BookId.Value, EducationProgramId = request.EducationProgramId.Value, StudentsInGroupsId = studentInGroup.Id, StudentRegisterNumber = request.StudentRegisterNumber.Value, Notes = request.Notes, EntryDate = request.EntryDate, ExamDate = request.ExamDate, ExamDateNumber = request.ExamDateNumber }; unitOfWork.GetGenericRepository <StudentRegisterEntry>().Add(entityToAdd); unitOfWork.Save(); if ((await GetById(request.BookId.Value)) .IsNotSuccess(out ActionResponse <StudentRegisterDto> bookResponse, out StudentRegisterDto registerDto)) { scope.Dispose(); return(await ActionResponse <StudentRegisterEntryDto> .ReturnError(bookResponse.Message)); } if (registerDto.NumberOfEntries >= 200) { if (request.CreateNewBook) { entityToAdd.StudentRegister.Full = true; unitOfWork.Save(); } else { registerDto.Full = true; if ((await Update(registerDto)).IsNotSuccess(out bookResponse, out registerDto)) { scope.Dispose(); return(await ActionResponse <StudentRegisterEntryDto> .ReturnError(bookResponse.Message)); } } } scope.Complete(); return(await ActionResponse <StudentRegisterEntryDto> .ReturnSuccess(mapper.Map <StudentRegisterEntry, StudentRegisterEntryDto>(entityToAdd))); } } catch (Exception) { return(await ActionResponse <StudentRegisterEntryDto> .ReturnError("Greška prilikom upisa u matičnu knjigu.")); } //finally //{ //var registerTask = cacheService.RefreshCache<List<StudentRegisterDto>>(); //var entryTask = cacheService.RefreshCache<List<StudentRegisterEntryDto>>(); //await Task.WhenAll(registerTask, entryTask); //} }
private async Task <ActionResponse <StudentRegisterEntryDto> > GetEntryForStudentNumberAndBookNumberAndBookYearDetailed(StudentRegisterEntryInsertRequest request) { try { var insertedStudent = unitOfWork.GetGenericRepository <StudentRegisterEntry>() .GetAll(sre => sre.StudentRegisterNumber == request.StudentRegisterNumber && sre.StudentRegister.BookNumber == request.BookNumber && sre.StudentRegister.BookYear == request.BookYear && sre.Status == DatabaseEntityStatusEnum.Active, includeProperties: "StudentsInGroups.Student," + "StudentsInGroups.Student.CityOfBirth,StudentsInGroups.Student.CountryOfBirth,StudentsInGroups.Student.RegionOfBirth," + "StudentsInGroups.Student.MunicipalityOfBirth,StudentsInGroups.Student.AddressCountry," + "StudentsInGroups.Student.AddressCity,StudentsInGroups.Student.AddressRegion,StudentsInGroups.Student.AddressMunicipality," + "StudentsInGroups.StudentGroup," + "StudentsInGroups.StudentGroup.ClassLocation.Country, StudentsInGroups.StudentGroup.ClassLocation.Region," + "StudentsInGroups.StudentGroup.ClassLocation.Municipality, StudentsInGroups.StudentGroup.ClassLocation.City," + "EducationProgram.EducationGroup, EducationProgram.Subjects," + "StudentsInGroups.StudentGroup.Director.UserDetails, StudentsInGroups.StudentGroup.Director.UserDetails.Signature," + "StudentsInGroups.StudentGroup.EducationLeader.UserDetails, StudentsInGroups.StudentGroup.EducationLeader.UserDetails.Signature") .FirstOrDefault(); return(await ActionResponse <StudentRegisterEntryDto> .ReturnSuccess(mapper.Map <StudentRegisterEntryDto>(insertedStudent))); } catch (Exception) { return(await ActionResponse <StudentRegisterEntryDto> .ReturnError("Greška prilikom dohvata zapisa po knjizi i broju studenta.")); } }
private async Task <ActionResponse <StudentRegisterEntryDto> > GetEntryForStudentNumberAndBookNumberAndBookYear(StudentRegisterEntryInsertRequest request) { try { var insertedStudent = unitOfWork.GetGenericRepository <StudentRegisterEntry>() .ReadAllActiveAsQueryable() .Where(sre => sre.StudentRegisterNumber == request.StudentRegisterNumber && sre.StudentRegister.BookNumber == request.BookNumber && sre.StudentRegister.BookYear == request.BookYear && sre.Status == DatabaseEntityStatusEnum.Active) .FirstOrDefault(); return(await ActionResponse <StudentRegisterEntryDto> .ReturnSuccess(mapper.Map <StudentRegisterEntryDto>(insertedStudent))); } catch (Exception) { return(await ActionResponse <StudentRegisterEntryDto> .ReturnError("Greška prilikom dohvata zapisa po knjizi i broju studenta.")); } }
private async Task <ActionResponse <List <StudentRegisterEntryDto> > > GetEntriesForStudentAndProgram(StudentRegisterEntryInsertRequest request) { try { var insertedStudents = unitOfWork.GetGenericRepository <StudentRegisterEntry>() .ReadAllActiveAsQueryable() .Where(sre => sre.EducationProgramId == request.EducationProgramId.Value && sre.StudentsInGroups.StudentId == request.StudentId.Value && sre.Status == DatabaseEntityStatusEnum.Active) .ToList(); return(await ActionResponse <List <StudentRegisterEntryDto> > .ReturnSuccess(mapper.Map <List <StudentRegisterEntryDto> >(insertedStudents))); } catch (Exception) { return(await ActionResponse <List <StudentRegisterEntryDto> > .ReturnError("Greška prilikom dohvata zapisa koji sadrže traženi program i studenta.")); } }
public async Task <ActionResponse <StudentRegisterEntryInsertRequest> > PrepareForInsert(StudentRegisterEntryInsertRequest request) { try { var alreadyInserted = unitOfWork.GetGenericRepository <StudentRegisterEntry>() .ReadAllActiveAsQueryable() .Where(e => e.EducationProgramId == request.EducationProgramId) .Any(e => e.StudentsInGroups.StudentId == request.StudentId); if (alreadyInserted) { return(await ActionResponse <StudentRegisterEntryInsertRequest> .ReturnWarning("Ovaj student već je unuesen u matičnu knjigu za izabrani program. Molimo provjerite podatke.")); } ActionResponse <List <StudentRegisterDto> > registerResponse; if (!request.BookNumber.HasValue) { if ((await GetAllNotFull()).IsNotSuccess(out registerResponse, out List <StudentRegisterDto> notFullRegisters)) { return(await ActionResponse <StudentRegisterEntryInsertRequest> .ReturnError(registerResponse.Message)); } if (notFullRegisters.Any(sr => sr.BookYear == request.BookYear)) { var selectedBook = notFullRegisters.OrderByDescending(b => b.BookNumber).FirstOrDefault(); request.BookNumber = selectedBook.BookNumber; request.BookYear = selectedBook.BookYear; request.BookId = selectedBook.Id; } else { if ((await GetAll()).IsNotSuccess(out registerResponse, out List <StudentRegisterDto> registers)) { return(await ActionResponse <StudentRegisterEntryInsertRequest> .ReturnError("Greška prilikom dohvata postojećih matičnih knjiga.")); } var minBookNumber = registers.Min(r => r.BookNumber) ?? 0; var maxBookNumber = registers.Max(r => r.BookNumber) ?? 0; List <int> fullList = Enumerable.Range(minBookNumber, maxBookNumber - minBookNumber + 1).ToList(); List <int> realList = registers.Select(r => r.BookNumber.Value).ToList(); var missingNums = fullList.Except(realList).ToList(); var nextAvailableNumber = missingNums.Min() + 1; request.BookNumber = nextAvailableNumber; request.CreateNewBook = true; } } else { var chosenBook = unitOfWork.GetGenericRepository <StudentRegister>() .ReadAllActiveAsQueryable() .Where(b => b.BookNumber == request.BookNumber && b.BookYear == request.BookYear) .FirstOrDefault(); if (chosenBook == null) { request.CreateNewBook = true; } request.BookId = chosenBook?.Id; } if (!request.StudentRegisterNumber.HasValue) { var fullList = Enumerable.Range((request.BookNumber.Value * 200) - 199, 200).ToList(); if (request.CreateNewBook) { request.StudentRegisterNumber = fullList.Min(); } else { if ((await GetById(request.BookId.Value)) .IsNotSuccess(out ActionResponse <StudentRegisterDto> bookResponse, out StudentRegisterDto registerDto)) { return(await ActionResponse <StudentRegisterEntryInsertRequest> .ReturnError(bookResponse.Message)); } request.StudentRegisterNumber = registerDto.FreeStudentRegisterNumbers.Min(); } } return(await ActionResponse <StudentRegisterEntryInsertRequest> .ReturnSuccess(request)); } catch (Exception) { return(await ActionResponse <StudentRegisterEntryInsertRequest> .ReturnError("Greška prilikom pripreme za unos matične knjige.")); } }