public async Task <bool> AddAsync(AddAccommodationFormModel accommodationForm)
        {
            try
            {
                if (!(await _context.Accommodations.AnyAsync(x => x.Caption == accommodationForm.Caption)))
                {
                    var accommodation = new Accommodation()
                    {
                        Caption     = accommodationForm.Caption,
                        Address     = accommodationForm.Address,
                        CityId      = accommodationForm.CityId,
                        Code        = accommodationForm.Code,
                        District    = accommodationForm.District,
                        FileId      = accommodationForm.FileId,
                        IsActivated = accommodationForm.IsActivated,
                        Number      = accommodationForm.Number
                    };

                    await _context.Accommodations.AddAsync(accommodation);

                    await _context.SaveChangesAsync();

                    return(true);
                }
                else
                {
                    throw new ReservationGlobalException(AccommodationServiceErrors.AddDuplicateAccommodationError);
                }
            }
            catch (Exception ex)
            {
                throw new ReservationGlobalException(AccommodationServiceErrors.AddAccommodationError, ex);
            }
        }
Exemple #2
0
        public async Task <ActionResult <Reservation> > Post([FromBody] Reservation reservation)
        {
            _context.Bookings.Add(reservation);
            await _context.SaveChangesAsync();

            return(CreatedAtAction(nameof(Get), new { id = reservation.Id }, reservation));
        }
        public async Task <ActionResult <Contact> > Post([FromBody] Contact contact)
        {
            _context.Contacts.Add(contact);
            await _context.SaveChangesAsync();

            return(CreatedAtAction(nameof(Get), new { id = contact.Id }, contact));
        }
Exemple #4
0
        public async Task <long> AddAsync(AddBookinationFormModel AddFomrModel)
        {
            try
            {
                if (!(await _context.Units.AnyAsync(x => x.Id == AddFomrModel.UnitId)))
                {
                    throw new ReservationGlobalException(BookinationServiceErrors.UnitNotFoundError);
                }

                var bookination = new Bookination()
                {
                    Description     = AddFomrModel.Description,
                    NationalityCode = AddFomrModel.NationalityCode,
                    EndDate         = AddFomrModel.EndDate,
                    FirstName       = AddFomrModel.FirstName,
                    LastName        = AddFomrModel.LastName,
                    Mobile          = AddFomrModel.Mobile,
                    StartDate       = AddFomrModel.StartDate,
                    UnitId          = AddFomrModel.UnitId,
                    ReserveCode     = Guid.NewGuid()
                };

                await _context.Bookinations.AddAsync(bookination);

                await _context.SaveChangesAsync();

                return(bookination.Id);
            }
            catch (Exception ex)
            {
                throw new ReservationGlobalException(BookinationServiceErrors.AddError, ex);
            }
        }
Exemple #5
0
        public async Task <IActionResult> PutContactType(int id, ContactType contactType)
        {
            if (id != contactType.Id)
            {
                return(BadRequest());
            }

            _context.Entry(contactType).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ContactTypeExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> Create([Bind("Id,reserverName,nameOfRoom,reserveDateBegin,reserveDateEnd,cost")] Reservation reservation)
        {
            DateTime start = reservation.reserveDateBegin;
            DateTime end   = reservation.reserveDateEnd;
            string   room  = reservation.nameOfRoom;


            var dbList = from r in _context.Reservation
                         where r.nameOfRoom == room
                         where r.reserveDateBegin <= end
                         where r.reserveDateEnd >= start
                         select r;

            List <int> reserveIds = dbList.Select(c => c.Id).Distinct().ToList();

            if (dbList.Any())
            {
                ModelState.AddModelError("reserveDateBegin", "There is a date conflict in our database with reservation ID's " + string.Join(", ", reserveIds) + ", check our calendar for availability!");
                return(View(reservation));
            }


            if (ModelState.IsValid)
            {
                _context.Add(reservation);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Calendar)));
            }
            return(View(reservation));
        }
        public async Task DeleteExpiredTokensAsync()
        {
            var now = DateTimeOffset.UtcNow;
            await _tokens.Where(x => x.RefreshTokenExpiresDateTime < now)
            .ForEachAsync(userToken =>
            {
                _tokens.Remove(userToken);
            });

            await _uow.SaveChangesAsync();
        }
        public async Task <Guid> UploadAsync(User user, IFormFile file)
        {
            try
            {
                if (string.IsNullOrEmpty(file.FileName) || file.Length == 0)
                {
                    throw new ReservationGlobalException(UploadServiceErrors.UploadFileValidError);
                }
                var extension = file.FileName.Split('.')[file.FileName.Split('.').Length - 1];
                var fileName  = Guid.NewGuid().ToString() + "." + extension;
                var path      = Path.Combine(_siteSettings.Value.UserAttachedFile.PhysicalPath, fileName);

                using (var bits = new FileStream(path, FileMode.Create))
                {
                    await file.CopyToAsync(bits);
                }
                var uploadFile = new FileAddress()
                {
                    FilePath     = path,
                    FileType     = extension,
                    UserId       = user.Id,
                    FileSize     = file.Length,
                    CreationDate = DateTime.Now,
                };
                await _context.FileAddresses.AddAsync(uploadFile);

                await _context.SaveChangesAsync();

                return(uploadFile.FileId);
            }
            catch (Exception ex)
            {
                throw new ReservationGlobalException(UploadServiceErrors.UploadFileError, ex);
            }
        }
        public async Task UpdateUserLastActivityDateAsync(Guid userId)
        {
            var user = await FindUserAsync(userId);

            if (user.LastLoggedIn != null)
            {
                var updateLastActivityDate = TimeSpan.FromMinutes(2);
                var currentUtc             = DateTimeOffset.UtcNow;
                var timeElapsed            = currentUtc.Subtract(user.LastLoggedIn.Value);
                if (timeElapsed < updateLastActivityDate)
                {
                    return;
                }
            }
            user.LastLoggedIn = DateTimeOffset.UtcNow;
            await _context.SaveChangesAsync();
        }
        public async Task <bool> SaveReservation(Reservation reservation)
        {
            await _context.Reservations.AddAsync(reservation);

            var success = await _context.SaveChangesAsync() > 0;

            if (success)
            {
                return(true);
            }
            throw new Exception("Problem saving the reservation");
        }
Exemple #11
0
        public async Task <long> AddGroupAsync(User user, AddGroupFormModel groupForm)
        {
            try
            {
                var sameGroup = await _context.GroupAuths.Where(c => c.Name == groupForm.Name).ToListAsync();

                if (sameGroup.Count > 0)
                {
                    throw new ReservationGlobalException(GroupServiceErrors.SameGroupExistError);
                }

                var group = new GroupAuth()
                {
                    Name        = groupForm.Name,
                    Description = groupForm.Description
                };
                _context.GroupAuths.Add(group);

                foreach (var gfRole in groupForm.RoleIds)
                {
                    var groupAuthRole = new GroupAuthRole()
                    {
                        RoleId      = gfRole,
                        GroupAuthId = group.Id
                    };
                    _context.GroupAuthRoles.Add(groupAuthRole);
                }
                await _context.SaveChangesAsync();

                return(group.Id);
            }
            catch (Exception ex)
            {
                throw new ReservationGlobalException(GroupServiceErrors.AddGroupError, ex);
            }
        }
        public async Task CreateRequestForCommandAsync <T>(Guid id)
        {
            var exists = await ExistAsync(id);

            var request = exists ?
                          throw new Exception($"Request with {id} already exists") :
                                new ClientRequest()
                                {
                                    Id   = id,
                                    Name = typeof(T).Name,
                                    Time = DateTime.UtcNow
                                };

            _context.Add(request);

            await _context.SaveChangesAsync();
        }
        public static void SeedUsers(UserManager <User> userManager, RoleManager <Role> roleManager,
                                     ReservationDbContext reservationDbContext)
        {
            if (userManager.Users.Any())
            {
                return;
            }

            var contactTypes = new List <ContactType>()
            {
                new ContactType {
                    Name = "ContactType #1"
                },
                new ContactType {
                    Name = "ContactType #2"
                },
                new ContactType {
                    Name = "ContactType #3"
                },
            };

            reservationDbContext.ContactTypes.AddRange(contactTypes);
            reservationDbContext.SaveChangesAsync().Wait();

            var roles = new List <Role>()
            {
                new Role()
                {
                    Name = Constants.RoleNameAdmin
                }
            };

            roles.ForEach(role => roleManager.CreateAsync(role).Wait());

            var adminUser = new User()
            {
                UserName = "******"
            };

            userManager.CreateAsync(adminUser, "Password*123").Wait();
            userManager.AddToRoleAsync(adminUser, Constants.RoleNameAdmin).Wait();
        }
        public async Task AddAsync(Domain.Models.Reservation reservation)
        {
            await _dbContext.Reservations.AddAsync(reservation);

            await _dbContext.SaveChangesAsync();
        }