Exemplo n.º 1
0
        public async Task <ActionResult <UULResponse> > NewUser(NewUserDTO newUser)
        {
            UULResponse response;

            try {
                if (!newUser.isValid(out var msg))
                {
                    return(Error.ProfileValidationFailed.CreateErrorResponse(_logger, "NewProfile", new Exception(msg)));
                }
                var exist = await _context.Users.AnyAsync(u => u.Login.Equals(newUser.Login) && u.ApartmentCode == newUser.ApartmentCode);

                if (exist)
                {
                    return(Error.ProfileAlreadyExists.CreateErrorResponse(_logger, "NewProfile"));
                }

                var user = UserDao.AddFromDto(_context, newUser);
                await _context.SaveChangesAsync();

                var userInfo    = new UserInfoDTO(user);
                var tokenString = SecHelper.GenerateJSONWebToken(userInfo.Login, userInfo.ApartmentCode, _config);

                response = new UULResponse()
                {
                    Success = true, Message = tokenString, Data = userInfo
                };
            } catch (Exception e) {
                response = Error.ProfileCreationFailed.CreateErrorResponse(_logger, "NewProfile", e);
            }
            return(response);
        }
Exemplo n.º 2
0
        public static void CreateDummyData(UULContext context)
        {
            for (int i = 0; i < 20; i++)
            {
                var user = new NewUserDTO()
                {
                    Login = "******" + i, AvatarSrc = "Default", Name = "Inhabitant_" + i, ApartmentCode = "T" + i, Pwd = "12345"
                };
                UserDao.AddFromDto(context, user);
            }
            context.SaveChanges();
            List <TimeSlot> timeSlots = new();
            var             today     = DateTime.UtcNow;

            for (int i = 0; i < 3; i++)
            {
                var slots = TimeSlotsFactory.CreateTimeSlotsForDateUTC(context, new DateTime(today.Year, today.Month, today.Day - i).ToUniversalTime(), 5);
                slots.Wait();
                timeSlots.AddRange(slots.Result);
            }
            var habitants = context.Habitants.ToList();
            var size      = habitants.Count;
            var rnd       = new Random();

            foreach (TimeSlot timeSlot in timeSlots)
            {
                for (int i = 0; i < rnd.Next(4); i++)
                {
                    if (timeSlot.OccupiedBy == null)
                    {
                        timeSlot.OccupiedBy = new List <Habitant>();
                    }
                    timeSlot.OccupiedBy.Add(habitants.ElementAt(rnd.Next(size)));
                }
            }
            context.TimeSlots.AddRange(timeSlots);

            var newsList = new List <News>();

            for (int i = 0; i < 5; i++)
            {
                newsList.Add(new News()
                {
                    Title = "Title " + i, Content = " Content " + i + dummyContent, Author = "Dummy data generator", CreatedAt = DateTime.UtcNow, Auditory = Auditory.GUESTS, NewsType = NewsType.INFO
                });
            }
            newsList.Add(new News()
            {
                Title = "Title Registered", Content = " Content Registered", Author = "Dummy data generator", CreatedAt = DateTime.UtcNow, Auditory = Auditory.REGISTERED, NewsType = NewsType.CALL_TO_ACTION
            });
            newsList.Add(new News()
            {
                Title = "Title Activated", Content = " Content Activated", Author = "Dummy data generator", CreatedAt = DateTime.UtcNow, Auditory = Auditory.ACTIVATED, NewsType = NewsType.ALERT
            });
            context.News.AddRange(newsList);
        }