public async Task <Code> Put(int id, Code itm)
        {
            if (id != itm.CodeId)
            {
                return(null);
            }

            var newCode = await _context.Codes.FindAsync(id);

            if (newCode == null)
            {
                return(null);
            }

            newCode.Name = itm.Name;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException) when(!Exists(id))
            {
                return(null);
            }

            return(newCode);
        }
        public async Task <CodeServiceUser> Post(CodeServiceUser itm)
        {
            var codeItm = await _context.Codes.FindAsync(itm.CodeId);

            var serviceItm = await _context.Services.FindAsync(itm.ServiceId);

            var username = CurrentUserName();

            if (codeItm == null || codeItm.CodeId != itm.CodeId ||
                serviceItm == null || serviceItm.ServiceId != itm.ServiceId ||
                username == null)
            {
                return(null);
            }

            var newItm = new CodeServiceUser
            {
                CodeId    = itm.CodeId,
                Code      = codeItm,
                ServiceId = itm.ServiceId,
                Service   = serviceItm,
                UserName  = username,
                Enabled   = itm.Enabled
            };

            _context.CodesServicesUsers.Add(newItm);
            await _context.SaveChangesAsync();

            return(newItm);
        }
Пример #3
0
        public async Task <Service> Put(int id, Service itm)
        {
            if (id != itm.ServiceId)
            {
                return(null);
            }

            var newItm = await _context.Services.FindAsync(id);

            if (newItm == null)
            {
                return(null);
            }

            newItm.Name        = itm.Name;
            newItm.Description = itm.Description;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException) when(!Exists(id))
            {
                return(null);
            }

            return(newItm);
        }
Пример #4
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public async void Configure(
            IApplicationBuilder app,
            IWebHostEnvironment env,
            PromocodesAppContext context,
            UserManager <ApplicationUser> userManager,
            IHttpContextAccessor httpContextAccessor)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();

                app.UseSwagger();
                app.UseSwaggerUI(c => {
                    c.SwaggerEndpoint("/swagger/v1/swagger.json", "PromocodesApp v1");
                    c.RoutePrefix = string.Empty;
                });

                context.Codes.Add(new Code {
                    Name = "Code 1"
                });
                context.Services.Add(new Service {
                    Name = "Service 1", Description = "Desc"
                });
                var userService = new UserService
                                      (userManager, Configuration, httpContextAccessor);
                await userService.Register(new RegisterRequest {
                    Email = "*****@*****.**", Username = "******", Password = "******"
                });

                await context.SaveChangesAsync();
            }

            // global cors policy
            app.UseCors(x => x
                        .SetIsOriginAllowed(origin => true)
                        .AllowAnyMethod()
                        .AllowAnyHeader()
                        .AllowCredentials());

            app.UseRouting();

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers().RequireAuthorization();
            });
        }